JavaFX switch scenes 💞

Поделиться
HTML-код
  • Опубликовано: 2 окт 2024
  • JavaFX switch scenes with using SceneBuilder tutorial example explained
    #javafx #switch #scenes
    //--------------------------------Main.java--------------------------------------
    package application;
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.stage.Stage;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    public class Main extends Application {
    @Override
    public void start(Stage stage) {
    try {
    Parent root = FXMLLoader.load(getClass().getResource("Scene1.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    } catch(Exception e) {
    e.printStackTrace();
    }
    }
    public static void main(String[] args) {
    launch(args);
    }
    }
    //---------------------------------SceneController.java---------------------------------------
    package application;
    import java.io.IOException;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Node;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    public class SceneController {
    private Stage stage;
    private Scene scene;
    private Parent root;
    public void switchToScene1(ActionEvent event) throws IOException {
    root = FXMLLoader.load(getClass().getResource("Scene1.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    }
    public void switchToScene2(ActionEvent event) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("Scene2.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    }
    }
    //------------------------------------------------------------------------------------------------
    Bro Code merch store 👟 :
    ===========================================================
    teespring.com/...
    ===========================================================
    music credits 🎼 :
    ===========================================================
    Up In My Jam (All Of A Sudden) by - Kubbi / kubbi
    Creative Commons - Attribution-ShareAlike 3.0 Unported- CC BY-SA 3.0
    Free Download / Stream: bit.ly/2JnDfCE
    Music promoted by Audio Library • Up In My Jam (All Of A...
    ===========================================================

Комментарии • 156

  • @BroCodez
    @BroCodez  3 года назад +33

    //--------------------------------Main.java--------------------------------------
    package application;

    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.stage.Stage;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    public class Main extends Application {

    @Override
    public void start(Stage stage) {
    try {

    Parent root = FXMLLoader.load(getClass().getResource("Scene1.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();

    } catch(Exception e) {
    e.printStackTrace();
    }
    }
    public static void main(String[] args) {
    launch(args);
    }
    }
    //---------------------------------SceneController.java---------------------------------------
    package application;
    import java.io.IOException;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Node;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    public class SceneController {
    private Stage stage;
    private Scene scene;
    private Parent root;

    public void switchToScene1(ActionEvent event) throws IOException {
    root = FXMLLoader.load(getClass().getResource("Scene1.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    }

    public void switchToScene2(ActionEvent event) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("Scene2.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
    }
    }
    //-----------------------------------------Scene1.fxml-------------------------------------------------












    //-----------------------------------------Scene2.fxml-------------------------------------------------












    //------------------------------------------------------------------------------------------------------

    • @catharperfect7036
      @catharperfect7036 6 месяцев назад

      I accidentally typed .getScene(scene); instead of .setScene(scene); and it took me 10 minutes to fix it.
      Am I doomed at coding?

    • @desmondkuffour397
      @desmondkuffour397 3 месяца назад

      @@catharperfect7036 you're doomed if you stop.

  • @saimhafeez4240
    @saimhafeez4240 3 года назад +18

    what about going back to scene 1 without making new scene ?

    • @aldolunabueno2634
      @aldolunabueno2634 5 месяцев назад +1

      This is a good question, because sometimes we want to our software to be less resource-intensive. This is my approach:
      public class SceneController {
      static private Stage stage;
      static private Scene scene1;
      static private Scene scene2;
      @FXML
      private void switchToScene1(ActionEvent event) {
      stage.setScene(scene1);
      stage.show();
      System.out.println("Stage: " + stage);
      System.out.println("Scene 1: " + scene1);
      }
      @FXML
      private void switchToScene2(ActionEvent event) throws IOException {
      if (scene2 == null) {
      Parent root = FXMLLoader.load(this.getClass().getResource("scene2.fxml"));
      scene2 = new Scene(root);
      scene1 = ((Node)event.getSource()).getScene();
      stage = (Stage) scene1.getWindow();
      }
      stage.setScene(scene2);
      stage.show();
      System.out.println("Stage: " + stage);
      System.out.println("Scene 2: " + scene2);
      }
      }

    • @lenaandthelambcampingadven3422
      @lenaandthelambcampingadven3422 3 месяца назад

      So this is how one should create a modular Ui? Scenes are a collection of linked Ui elements

  • @kaisander5672
    @kaisander5672 3 года назад +9

    Its a great tutorial and all but theres a lot of stuff that i don't understand that wasn't explained... Especially the five lines of code that are in the methods for switching scene. I would've liked it a lot more if there was a bit more explanation as to why you wrote the lines that you did. thanks

  • @andrijakolenda8161
    @andrijakolenda8161 2 года назад

    Really appreciate the video, even though scene builder isn't popular content. Helped me a ton brother!

  • @JLSXMK8
    @JLSXMK8 3 года назад +15

    You literally uploaded this just in time!!! I was wondering how I could switch scenes in JavaFX, because I'll need to work on a project coming up. Let me tell you, if the stuff in this video works on my PC, you've just saved the entire thing for me! Thank you!

  • @CHITUS
    @CHITUS 2 года назад +1

    Love this video. I was wondering how to do this switching the scenes, now thanks to you I'm enlightened:)

  • @learntoswim512
    @learntoswim512 2 года назад +3

    Any reason why you don't need to add @FXML before the switchToScene1 and 2 function? I had to to make it work. Thanks.

  • @nisarshaikh-e6o
    @nisarshaikh-e6o 5 месяцев назад

    i like your video,i am learing javafx from you

  • @johnmcafee9169
    @johnmcafee9169 Год назад +1

    this course is really helpful

  • @ernanfer2
    @ernanfer2 2 года назад

    I like all your videos about javaFx ...thanks for share

  • @peterg.1821
    @peterg.1821 Год назад

    I appreciate your video! It provided me with significant assistance. Greetings from Vienna!

  • @selekt7
    @selekt7 Год назад +1

    Ehi, I updated some line and extended this to other two classes and it worked:
    public abstract class SceneController {
    private Stage stage;
    private Scene scene;
    private FXMLLoader fxmlLoader;
    public void goToGame(ActionEvent event) throws IOException {
    fxmlLoader = new FXMLLoader(Application.class.getResource("scene1.fxml"));
    stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    scene = new Scene(fxmlLoader.load());
    stage.setScene(scene);
    stage.show();
    }
    public void goToMenu(ActionEvent event) throws IOException {
    fxmlLoader = new FXMLLoader(Application.class.getResource("scene2.fxml"));
    stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    scene = new Scene(fxmlLoader.load());
    stage.setScene(scene);
    stage.show();
    }
    }
    public class MenuController extends SceneController {
    @Override
    public void goToGame(ActionEvent event) throws IOException {
    super.goToGame(event);
    }
    }

  • @Kennerdoll
    @Kennerdoll Год назад +1

    3:08 - I'm kindly asking if you could explain what you mean by "CASTING" in the context you are using it.

  • @anandbmuley
    @anandbmuley 3 года назад

    Thank You @Bro Code. Finally I found a simple solution. Thank for sharing this video :)

  • @ctluwua7695
    @ctluwua7695 3 года назад +1

    Parent root = new FXMLLODER.BLA BLA bla bla bla bla bla bla bla 😂😂😂😂😂😂😂

  • @misharatkevich9808
    @misharatkevich9808 2 года назад +2

    HOLY FUCKING SHIT, the scene/screen switch is EXACTLY the problem that stumped me earlier on my first serious project, so much so that it really killed my motivation at that time, and everything I could find on the subject was something monstrously complicated and big that I couldn't adapt and make work.
    I'll need to test this myself, of course, but... this looks so concise. This looks REALLY concise. I still have the question of "can this work if each of the two methods is in its own separate controller?" (so that you don't have to have the same controller for all the scenes). but other than that... this is so concise. And the only complex part that I'm not fully understanding yet is the Stage/Node/event cast, which is a lot of casting and chaining and I'll need to read the docs more carefully to see how and why this works, but if this works out for me... holy shit, this is what I needed. I think this is what I needed.
    Thanks a lot, man.
    EDIT: Update, did it slightly differently (with some other help), but in a similar way. I'm not sure if this is the best option, but this helped A LOT. Thank you.

    • @hunchortw
      @hunchortw 2 года назад

      do you mind helping me out by saying what way you did it? because i can't get the scenes to swtich, it gives me pointer null error.

    • @StarDust4866
      @StarDust4866 2 года назад

      @@hunchortw Not sure you'll see this but what solved it for me was putting the fxml files in the resources file. No problems after that.

  • @malikshahzaib3313
    @malikshahzaib3313 3 года назад +5

    Hi bro thanks. Bro, Make a video on roadmap for java.

  • @alesto8962
    @alesto8962 3 года назад +5

    Love this video. I was wondering how to do this switching the scenes, now thanks to you I'm enlightened:)

  • @brahimage
    @brahimage 6 месяцев назад +1

    me and you against my comp engineering degree

  • @ctluwua7695
    @ctluwua7695 3 года назад +1

    YAY

  • @carolinezeiler8112
    @carolinezeiler8112 2 года назад +3

    You just saved the day (23:25). Been working on this all afternoon, my head is going to explode. Been looking in all my books, been watching all my usual tutorials and some others, your code is the only one that actually worked. THANKS ++++ I learned that you do not have access to the Controller Panel on the bottom left part of the SceneBuilder when you open it in IntelliJ. You can only access it within SceneBuilder itself.

  • @oussamajmaa9205
    @oussamajmaa9205 2 года назад +1

    thank you so much it works perfectly , but can i send data from scenes using this method ?

  • @alp4125
    @alp4125 4 месяца назад

    Why are we pass a ActionEvent reference to function parameter?We did do this too in the 6th video of in your JAVAfx playlist like that we use that here?Nonethless we are don't use a ActionEvent ,Why do we define ActionEvent?Please clarify my question anybody:)

  • @infern0824
    @infern0824 5 месяцев назад

    Can anyone PLEASE tell how can we do this: I have a log in page including a username button, if the user enters the admin’s name the log in button switches to the admin scene, and if the user enters another name, the log in button would take it to another scene?

  • @techhub_
    @techhub_ Год назад

    Amazing, you made things so easy!

  • @MaryamPrhn
    @MaryamPrhn 10 месяцев назад

    great!!

  • @gamingman0082
    @gamingman0082 Год назад

    SOMEONE PLEASE HELP ! When i am in JAVA FX Scene Builder and i click On Action in the Code settings of the button, the two options of "Switch to scene" doesn't pop up. The code in Java is written very good, i also copy-pasted it from the description. What can be wrong? Please help

  • @jitsevanparys1337
    @jitsevanparys1337 2 года назад

    Quick question, I got the error 'Caused by: javafx.fxml.LoadException: Root hasn't been set. Use method setRoot() before load.' Do you know how I can fix this?

  • @huzaifaalam8358
    @huzaifaalam8358 3 месяца назад

    Thank you very much for this video. I have been struggling for the past two nights and could not find the solution until I watched this one.

  • @gilantonyborba3616
    @gilantonyborba3616 11 месяцев назад

    Thank youuuuu!!!

  • @ionminascurta3274
    @ionminascurta3274 Год назад

    I try to switch some scenes that are in fullscreen mode, but isn't working even if I set Fullscreen on true before showing it.

  • @ShadowSilverWolf
    @ShadowSilverWolf Год назад

    bro that scam i can't even make 2 fxml have the same class as contral

  • @tx6723
    @tx6723 Год назад

    How can I use it in from a different controller in a different pac

  • @wesleybritovlk
    @wesleybritovlk 11 месяцев назад

    You are beautiful! THANK YOU BRO!

  • @MILENA-mh2wf
    @MILENA-mh2wf 6 месяцев назад

    that’s a great video! thanks u really help me)

  • @raihanfulviansyah
    @raihanfulviansyah Год назад

    gg bang,karena video ini tugas akhir sekolahku telah berjalan😁😁

  • @kaneshirosora1707
    @kaneshirosora1707 3 года назад +2

    how the sceneController class is connected to main method ? from what i saw is we didnt inherit or declare any method from sceneController class into the main method. or is it because of the Scene1.fxml file connected to the sceneController class ?

    • @onfued3088
      @onfued3088 5 месяцев назад

      the Scene1.fxml file connected to the sceneController class

  • @chrisalbers1370
    @chrisalbers1370 3 года назад +1

    Finally got it!!! Thank you my dudee!!!!

  • @realrrnc
    @realrrnc 3 года назад

    Can u make with scene builder bec I can not download it. Only one video with those plsssss

  • @Jhonyhndz
    @Jhonyhndz 10 месяцев назад

    Muchas gracias. Te felicito, tienes una forma muy agradable de enseñar.

  • @JamesBrodski
    @JamesBrodski Год назад

    Thank you so much! This helped a lot! God bless :)

  • @Arxa93
    @Arxa93 2 года назад

    Another comment because you're saving my university life

  • @SylphietteGreyrat-p9d
    @SylphietteGreyrat-p9d Год назад

    Hello BRO , What if the switch of scene doesn't trigger with an event. I'm trying to change a scene with a countdown timer. Any thoughts? My code is below:
    int i = 20;
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle){

    text.setText(String.valueOf(i));
    Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1),e ->{
    i--;
    text.setText(String.valueOf(i));
    if(text.getText().equalsIgnoreCase("0")) {
    System.out.print("GG");
    nextQuestionArrow.setVisible(true);
    textFieldAnswer.setEditable(false);
    text.setVisible(false);


    Parent root = FXMLLoader.load(getClass().getResource("Scene13BRoomAndPillarQuestion2.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();


    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();

    root = null;
    stage = null;
    scene = null;

    }
    }));
    }

  • @sushantlama9010
    @sushantlama9010 3 года назад

    Searched everywhere but couldn't find , but here it was hidden.

  • @lorenzogradaschi3711
    @lorenzogradaschi3711 2 года назад

    is this also showing the stage in the same window or not? or is creating another window?

  • @fidam1483
    @fidam1483 2 года назад

    Hello is it possible to switch from an fxml scene to an xml scene with the same way please ?

  • @mustaphaELHOURA
    @mustaphaELHOURA 3 года назад +2

    The best teacher ever, thank you!

  • @ApenDover
    @ApenDover 2 года назад

    How to do that fullscreen for both stages with initialize func?

  • @krisinfo7823
    @krisinfo7823 3 месяца назад

    Excellent!! Thank you!1

  • @pay4ok983
    @pay4ok983 Год назад

    Which version sdk do you use and you use maven or gradle

  • @maealcantara5147
    @maealcantara5147 Год назад

    Does the other scene closes when it switches to another scene?

  • @alesto8962
    @alesto8962 2 года назад

    Im back to this video with additional question. Bro how to measure the time between switching scenes in mili or nanoseconds? I know for this example it is nearly 0 but for more complicated memory-eating scenes?

  • @mariagabrielamaiz3878
    @mariagabrielamaiz3878 5 месяцев назад

    100% useful for begginers 😁 thanks

  • @crazy-forever
    @crazy-forever 4 месяца назад

    It was soo helpful and fun toooo

  • @ShinduPK4
    @ShinduPK4 2 года назад

    Bro I'm writing a quick, trash GUI for a project and you've been a LIFE SAVER. Love you

  • @davidm6822
    @davidm6822 3 года назад

    Hello Bro,
    I´m currently trying to use your method to connect three different scenes with each other but it isn´t working as wished. Maybe you can give me a hint or maybe make a video on that? :)

  • @imadnosairat480
    @imadnosairat480 Год назад

    thank you very much :D
    from deep of my heart

  • @amadoutraore1967
    @amadoutraore1967 5 месяцев назад

    I am now learning all from you Thank you once again

  • @chemso_1
    @chemso_1 5 месяцев назад

    da best

  • @MorsHs
    @MorsHs 3 года назад

    Hi i have some question.. Iam new in programming but what is the difference of the "Pane" and "Parent" . Using a Scene Builder

  • @aljhonbautista1344
    @aljhonbautista1344 2 года назад

    How abouy switching multiples scenes

  • @wolanus
    @wolanus 3 года назад

    Great video!

  • @Surgeryxperts
    @Surgeryxperts 3 года назад

    Hey bro can you please make a tetris game in java

  • @katt2478
    @katt2478 2 года назад

    is it the same code if we're using netbeans ?

  • @tarblez6260
    @tarblez6260 5 месяцев назад

    thx

  • @alexanderzharkov6953
    @alexanderzharkov6953 3 года назад

    You are the best. Keep going. I am glad I've found your channel

  • @lolyabmasle99
    @lolyabmasle99 5 месяцев назад

    I did it, man thanks!

  • @Ryokousuki
    @Ryokousuki 2 года назад

    @Bro Code Im work on a project that uses java fx and i want to implement multi windows, i tried your code out, or anybodies code for that matter, but i always get that the fmxlloader line says null pointer exception. I have all of my classes and fxml files in the same place as yours, but i dont the problem, ive tried multiple attemps.

    • @hunchortw
      @hunchortw 2 года назад

      same issue. Did you find a solution bro?

    • @Ryokousuki
      @Ryokousuki 2 года назад

      @@hunchortw I gave up and just hard coded without screenbuilder, I just used hiding and showing to replace the windows.

  • @elmanuele6558
    @elmanuele6558 2 года назад

    thank you, just thanks for this video ❤

  • @mr.RAND5584
    @mr.RAND5584 2 года назад

    Switch scene using the menu click?

  • @piezadigital
    @piezadigital Год назад

    Encontré lo que necesitaba en este video, gracias.

  • @oleksandr3275
    @oleksandr3275 2 года назад

    Thank you. It helped me :)

  • @GerardCastro-mg3wq
    @GerardCastro-mg3wq 4 месяца назад

    Nice video man !

  • @eliasbergmann7356
    @eliasbergmann7356 2 года назад

    Love your content! this really helped! could you make a tutorial on Windowbuilder?

  • @alexmartinezmunoz94
    @alexmartinezmunoz94 10 месяцев назад

    Thank you 😭💕💞

  • @alfredochola5971
    @alfredochola5971 2 года назад

    Thanks bro😃😂

  • @ouassimassoufi5962
    @ouassimassoufi5962 Год назад

    i appreciate it tho

  • @SuMyatHoney
    @SuMyatHoney 3 года назад

    Hi thanks for the tutorial I am subing

  • @alessioolivieri5460
    @alessioolivieri5460 2 года назад

    Thanki you very interesting

  • @DjojoElMaestro
    @DjojoElMaestro 9 месяцев назад

    ty

  • @AdamOndryas
    @AdamOndryas 11 месяцев назад

    great ivd bor

  • @albertovigna1268
    @albertovigna1268 2 года назад

    GR8 VIDEO M8!
    THANKS!!!

  • @siavashnourmohammadi9189
    @siavashnourmohammadi9189 Год назад

    thank you bro

  • @hamzachiguer642
    @hamzachiguer642 Год назад

    Thank you bro

  • @VicMarLL
    @VicMarLL 4 месяца назад

    thanks dude

  • @rassimve4873
    @rassimve4873 9 месяцев назад

    bro's the best

  • @JuanaPaivaTavares
    @JuanaPaivaTavares 11 месяцев назад

    Thank you!!

  • @nathanaelnaouetchakote7290
    @nathanaelnaouetchakote7290 2 года назад

    why Node(l.20)?

  • @darth_kenobi4136
    @darth_kenobi4136 2 года назад

    int comment1;

  • @muneermjmuneer1099
    @muneermjmuneer1099 Год назад

    Thanks a lot

  • @asfsvn
    @asfsvn Год назад

    thanks bro

  • @StrawberryLatteMeow
    @StrawberryLatteMeow 2 года назад

    Thank you so much so the Javafx tutorials! you are a lifesaver

  • @audryespinoza8153
    @audryespinoza8153 2 года назад

    Muchas gracias por el codigo, me ayudo mucho a entender este procesoooo
    Thank you sooo much

  • @DesireEzire
    @DesireEzire Месяц назад

    comment

  • @juzerbaatwala8868
    @juzerbaatwala8868 10 месяцев назад

    superb!

  • @halcyon-s
    @halcyon-s Год назад

    thanks!

  • @darklava270
    @darklava270 Год назад

    comment

  • @piotrrymarz4536
    @piotrrymarz4536 2 года назад

    thanks

  • @aminrezaabbasi7813
    @aminrezaabbasi7813 2 года назад

    That was great, thank you

  • @chocodonuts3644
    @chocodonuts3644 10 месяцев назад

    WOW you are amazing