0

My school team and I were tasked to create a desktop app. We want to code without the use of FXML and we were unsure how to switch the scenes. How would the stage be referenced then? Any sample skeleton code provided would be great.

Currently, the main GUI looks something like this. I was told that I could actually use the login/home controller to display its login instead of using static methods in the main GUI.

public class Main extends Application {
.....
    public static void displayHome() {
        home.getStylesheets().add("css/MainCSS.css");
        window.setScene(home);

    }

    public static void displayLogin() {
        login.getStylesheets().add("css/MainCSS.css");
        window.setScene(login);
    }

   public static void main(String[] args) {
        launch(args);
    }
...
...
.....

}
Tracy
  • 1
  • [mcve] please.. – kleopatra Oct 29 '20 at 04:45
  • 1
    btw: static scope is wrong, nearly always – kleopatra Oct 29 '20 at 10:29
  • “How would the stage be referenced then?”. You can get the scene containing any node (eg a button) using `button.getScene()` and then get the window by calling `getWindow()` on the scene. – James_D Oct 29 '20 at 11:03
  • [Here](https://stackoverflow.com/questions/36868391/using-javafx-controller-without-fxml/36873768#36873768) is an example (not directly relevant to your question) of using MVC without FXML. You can create a “view model” which uses a JavaFX property to maintain the currently visible view (perhaps as an enum) and the observe that property in your main view, switching scenes when it changes. – James_D Oct 29 '20 at 11:06
  • And also, instead of switching scenes, consider using a single scene and switching the root of that scene instead. That will save you from having to set the stylesheet each time (and has other benefits, such as preserving "full screen" status). – James_D Oct 29 '20 at 11:47

1 Answers1

0

Inside the class -->

Scene Scene_1, Scene_2;

Scene_1= new Scene(AnchorPane1, 550, 500);
Scene_2= new Scene(AnchorPane2, 550, 500);

You can use any button inside the anchorpane of any scene to go to the next scene by using setOnAction command.

Something like command below -

Button1.setOnAction(e -> primaryStage.setScene(Scene_2));
Button2.setOnAction(e -> primaryStage.setScene(Scene_1));
vinvin
  • 31
  • 4