How to open another window in JavaFX 2?
Asked Answered
B

2

16

I have made a simple JavaFX application, but I would like the main window to open a secondary window when the user clicks a button.

What would be the simplest way to accomplish this?

Brahmani answered 7/1, 2014 at 4:4 Comment(1)
Register an event handler with your button. In that event handler, you'll do much the same as you did for your main window. The only real difference is that you have to create the Stage yourself. Try it and post some code showing where you are stuck and/or what is not working as you expect.Katzman
K
31
Button b = new Button();
b.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent e) {
        Stage stage = new Stage();
        //Fill stage with content
        stage.show();
    }
});
Kalinin answered 7/1, 2014 at 8:39 Comment(0)
I
1

try this


try {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXML.fxml"));
    Parent root1 = (Parent) fxmlLoader.load();
    Stage stage = new Stage();
    stage.setScene(new Scene(root1));
    stage.show();
    ((Node) (event.getSource())).getScene().getWindow().hide();
} catch (Exception e) {
    e.printStackTrace();
}
Insecure answered 1/10, 2019 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.