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?

Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
  • 3
    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. – James_D Jan 07 '14 at 04:11

2 Answers2

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();
    }
});
Alexander Kirov
  • 3,624
  • 1
  • 19
  • 23
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();
}