Display two windows at the same time using JavaFX Scene Builder 2.0


Question

I'm working on a mini application where I need to display to users 2 windows at the same time.

I'm working with JavaFx Scene Builder 2.0 on NetBeans 8.0.1

is it possible to do this? if so, how it can be done ?

Thank you!

1
2
10/15/2014 2:37:18 PM

Accepted Answer

By "screen" I assume you mean "window".

Just create a second stage in your start() method and do exactly the same with it as you do your primary stage:

public class MyApp extends Application {

    @Override
    public void start(Stage primaryStage) {

        Stage anotherStage = new Stage();

        try {
            FXMLLoader loader = new FXMLLoader(...); // FXML for primary stage
            Parent root = loader.load();
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.show();

            FXMLLoader anotherLoader = new FXMLLoader(...) ; // FXML for second stage
            Parent anotherRoot = anotherLoader.load();
            Scene anotherScene = new Scene(anotherRoot);
            anotherStage.setScene(anotherScene);
            anotherStage.show();

        } catch (Exception exc) {

            exc.printStackTrace();

        }
    }

    public static void main(String[] args) { launch(args); }
}
3
10/14/2014 7:51:35 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon