AyyLmaokai
AyyLmaokai

Reputation: 27

Creating multiple screens in javaFX

Im making an kinda information like application for a city and I would like to use either multiple or a scene that gets updated. Is this possible and if so any tips?

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Informative Program");
    primaryStage.setScene(new Scene(root, 1000, 600));
    primaryStage.show();
}


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

}

Upvotes: 0

Views: 1106

Answers (1)

XaolingBao
XaolingBao

Reputation: 1044

The beauty of JavaFX is that you can add listeners to your application to dynamically change any part of your application, including the entire scene.

By using your example you can do

    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); 

    scene.setRoot(root);`

    root = FXMLLoader.load(getClass().getResource("sample2.fxml")); 
    scene.setRoot(root2);

Essentially you just need to load a need root and then set it, and you should be good to go.

I use this personally for an Application I've been writing where the users log in first, and then it changes the root node to the next screen after login.

You could change your root at any point, or you could add/remove elements from the root, at any point, as well.

JavaFX is great in how Dynamic/Flexible it is as a language/Framework

Upvotes: 1

Related Questions