JavaFX switch scenes 💞

Поділитися
Вставка
  • Опубліковано 21 сер 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 4 місяці тому

      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 Місяць тому

      @@catharperfect7036 you're doomed if you stop.

  • @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!

  • @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.

  • @kaisander5672
    @kaisander5672 3 роки тому +8

    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

  • @huzaifaalam8358
    @huzaifaalam8358 2 місяці тому

    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.

  • @misharatkevich9808
    @misharatkevich9808 2 роки тому +1

    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.

  • @saimhafeez4240
    @saimhafeez4240 3 роки тому +18

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

    • @aldolunabueno2634
      @aldolunabueno2634 3 місяці тому +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 Місяць тому

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

  • @alesto8962
    @alesto8962 3 роки тому +5

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

  • @user-ct5us8xg5v
    @user-ct5us8xg5v 3 місяці тому

    i like your video,i am learing javafx from you

  • @ernanfer2
    @ernanfer2 2 роки тому

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

  • @mustaphaELHOURA
    @mustaphaELHOURA 3 роки тому +2

    The best teacher ever, thank you!

  • @ShinduPK4
    @ShinduPK4 2 роки тому

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

  • @andrijakolenda8161
    @andrijakolenda8161 2 роки тому

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

  • @amadoutraore1967
    @amadoutraore1967 4 місяці тому

    I am now learning all from you Thank you once again

  • @mariagabrielamaiz3878
    @mariagabrielamaiz3878 4 місяці тому

    100% useful for begginers 😁 thanks

  • @realrrnc
    @realrrnc 3 роки тому

    The best one who do programming

  • @rolakhaled5070
    @rolakhaled5070 3 місяці тому

    amazingley amazing

  • @chemso_1
    @chemso_1 4 місяці тому

    da best

  • @johnmcafee9169
    @johnmcafee9169 Рік тому +1

    this course is really helpful

  • @alexanderzharkov6953
    @alexanderzharkov6953 2 роки тому

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

  • @chrisalbers1370
    @chrisalbers1370 3 роки тому +1

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

  • @crazy-forever
    @crazy-forever 3 місяці тому

    It was soo helpful and fun toooo

  • @GerardCastro-mg3wq
    @GerardCastro-mg3wq 3 місяці тому

    Nice video man !

  • @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.

  • @annexsoftworks
    @annexsoftworks 2 роки тому

    Excellent vid

  • @AdamOndryas
    @AdamOndryas 10 місяців тому

    great ivd bor

  • @englishgrammartutorial9618
    @englishgrammartutorial9618 3 роки тому

    Go ahead,excellent

  • @rassimve4873
    @rassimve4873 8 місяців тому

    bro's the best

  • @juzerbaatwala8868
    @juzerbaatwala8868 9 місяців тому

    superb!

  • @xXEkoLogikoXx
    @xXEkoLogikoXx 2 роки тому

    Another comment because you're saving my university life

  • @TheGreat8220
    @TheGreat8220 2 роки тому

    Keep up the awesome work!

  • @MILENA-mh2wf
    @MILENA-mh2wf 4 місяці тому

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

  • @peterg.1821
    @peterg.1821 Рік тому

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

  • @michaelsd28
    @michaelsd28 2 роки тому

    the way you explain is awesome! thank you!

  • @kenkinyua7036
    @kenkinyua7036 2 роки тому

    much respect and gratitude to you bro💯

  • @krisinfo7823
    @krisinfo7823 2 місяці тому

    Excellent!! Thank you!1

  • @malikshahzaib3313
    @malikshahzaib3313 3 роки тому +5

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

  • @raihanfulviansyah
    @raihanfulviansyah Рік тому

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

  • @StrawberryLatteMeow
    @StrawberryLatteMeow Рік тому

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

  • @ahmedshafiq9074
    @ahmedshafiq9074 2 роки тому

    good work

  • @AbhijeetKumar-cm3jh
    @AbhijeetKumar-cm3jh 3 роки тому

    Loving this Series

  • @ctluwua7695
    @ctluwua7695 3 роки тому +1

    YAY

  • @ashkanshakiba9579
    @ashkanshakiba9579 3 роки тому

    Useful 👌👍

  • @MaryamPrhn
    @MaryamPrhn 8 місяців тому

    great!!

  • @ozy_drag5309
    @ozy_drag5309 Рік тому

    this guys the goat

  • @chocodonuts3644
    @chocodonuts3644 9 місяців тому

    WOW you are amazing

  • @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.

  • @brahimage
    @brahimage 5 місяців тому +1

    me and you against my comp engineering degree

  • @kimevar
    @kimevar 3 роки тому

    Nice videos keep up the good work!

  • @romanmatton2636
    @romanmatton2636 10 місяців тому

    nice video

  • @David-hv9zn
    @David-hv9zn 7 місяців тому

    👍

  • @michaelsd28
    @michaelsd28 2 роки тому

    great tutorial

  • @cadamsmith
    @cadamsmith 2 роки тому

    great content, exactly what I was looking for

  • @Bruh-405
    @Bruh-405 Рік тому

    Bussin

  • @anandbmuley
    @anandbmuley 2 роки тому

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

  • @alexmartinezmunoz94
    @alexmartinezmunoz94 8 місяців тому

    Thank you 😭💕💞

  • @jasperb.2003
    @jasperb.2003 2 роки тому

    Awesome

  • @elmanuele6558
    @elmanuele6558 2 роки тому

    thank you, just thanks for this video ❤

  • @JamesBrodski
    @JamesBrodski Рік тому

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

  • @albertovigna1268
    @albertovigna1268 2 роки тому

    GR8 VIDEO M8!
    THANKS!!!

  • @techhub_
    @techhub_ Рік тому

    Amazing, you made things so easy!

  • @mayurpatel481
    @mayurpatel481 2 роки тому

    Really helpful. Thanks a lot.

  • @mikoo_k
    @mikoo_k Рік тому

    bro , you are so good

  • @sushantlama9010
    @sushantlama9010 3 роки тому

    Life saver❤️❤️❤️❤️❤️❤️🙏👍

  • @hasankhani6285
    @hasankhani6285 2 роки тому

    Best

  • @imadnosairat480
    @imadnosairat480 Рік тому

    thank you very much :D
    from deep of my heart

  • @SuMyatHoney
    @SuMyatHoney 3 роки тому

    Hi thanks for the tutorial I am subing

  • @wolanus
    @wolanus 3 роки тому

    Great video!

  • @fahengraphic7743
    @fahengraphic7743 11 місяців тому

    Perfect man, thanks a lot

  • @lolyabmasle99
    @lolyabmasle99 4 місяці тому

    I did it, man thanks!

  • @wesleybritovlk
    @wesleybritovlk 10 місяців тому

    You are beautiful! THANK YOU BRO!

  • @Jhonyhndz
    @Jhonyhndz 8 місяців тому

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

  • @oleksandr3275
    @oleksandr3275 2 роки тому

    Thank you. It helped me :)

  • @danielsballs
    @danielsballs Рік тому

    helped a lot, thanks!

  • @sweetlies1937
    @sweetlies1937 3 роки тому

    Thank you so much, bro! x)

  • @viacheslavsvo4928
    @viacheslavsvo4928 2 роки тому

    It's great thx!

  • @LampaHQ
    @LampaHQ 2 роки тому

    Wonderful, thank you

  • @aminrezaabbasi7813
    @aminrezaabbasi7813 2 роки тому

    That was great, thank you

  • @muneermjmuneer1099
    @muneermjmuneer1099 Рік тому

    Thanks a lot

  • @piezadigital
    @piezadigital Рік тому

    Encontré lo que necesitaba en este video, gracias.

  • @gilantonyborba3616
    @gilantonyborba3616 10 місяців тому

    Thank youuuuu!!!

  • @alfredochola5971
    @alfredochola5971 2 роки тому

    Thanks bro😃😂

  • @VicMarLL
    @VicMarLL 3 місяці тому

    thanks dude

  • @hamzachiguer642
    @hamzachiguer642 Рік тому

    Thank you bro

  • @JuanaPaivaTavares
    @JuanaPaivaTavares 9 місяців тому

    Thank you!!

  • @alessioolivieri5460
    @alessioolivieri5460 2 роки тому

    Thanki you very interesting

  • @oussamajmaa9205
    @oussamajmaa9205 2 роки тому +1

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

  • @audryespinoza8153
    @audryespinoza8153 2 роки тому

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

  • @siavashnourmohammadi9189
    @siavashnourmohammadi9189 Рік тому

    thank you bro

  • @asfsvn
    @asfsvn Рік тому

    thanks bro

  • @2233zzwei
    @2233zzwei 3 роки тому

    Great video! Can you do a JavaScript tutorial?

  • @-gamingmd-1630
    @-gamingmd-1630 2 роки тому

    great, thank you!

  • @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);
    }
    }

  • @eliasbergmann7356
    @eliasbergmann7356 2 роки тому

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

  • @halcyon-s
    @halcyon-s Рік тому

    thanks!

  • @mahmoudnasr8710
    @mahmoudnasr8710 Рік тому

    Thanks bro 👌

  • @DjojoElMaestro
    @DjojoElMaestro 8 місяців тому

    ty

  • @felipe_6041
    @felipe_6041 Рік тому

    Very well!!!! Muito bom!!!!
    Legal! Legal!

  • @mehdiel7218
    @mehdiel7218 2 роки тому

    Thank you Dear

  • @saurabhatre2158
    @saurabhatre2158 Рік тому

    Thanks Thanks 😊