Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX + Scene Builder how switch scene

I'm working with JavaFx and Scenebuilder and want create a local app for myself called "Taskplanner" in eclipse.

I created a new Stage and set it with a Scene (see Main.java). But not sure how to set a new Scene in the old stage (see Controller.java). Didnt also not find out if it is possible pass the signInButtonClicked()-Methode the "Stage primaryStage" over Scene Builder

Can anybody help ?

Controller.java:

@FXML
Button btnSignIn;

@FXML
public void signInButtonClicked() throws Exception
{
//Here I want call the new Scene(SignInGUI.fxml) in my old Stage
   FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/SignInGUI.fxml"));
}

Main.java:

        @Override
        public void start(Stage primaryStage) throws Exception 
      {
            Parent root = FXMLLoader.load(getClass().getResource("../view/LoginGUI.fxml"));

            primaryStage.setTitle("Taskplanner");
            primaryStage.setScene(new Scene(root,500,500));
            primaryStage.show();
        }


        public static void main(String[] args) {

            launch(args);
    }
like image 976
Sam S. Avatar asked Jan 18 '16 20:01

Sam S.


1 Answers

You can get a reference to the Scene and Window from your button reference. From there, it's up to you to decide how to you want to show the new view.

Here's how you get those references:

Scene scene = btnSignIn.getScene();
Window window = scene.getWindow();
Stage stage = (Stage) window;

You can change the view by changing the root of your Scene:

FXMLLoader loader = ... // create and load() view
btnSignIn.getScene().setRoot(loader.getRoot());

Or you can change the entire Scene:

FXMLLoader loader = ... // create and load() view
Stage stage = (Stage) btnSignIn.getScene().getWindow();
Scene scene = new Scene(loader.getRoot());
stage.setScene(scene);
like image 174
Cypher Avatar answered Nov 15 '22 08:11

Cypher