How to close all stages when the primary stage is closed

javafx-2

I have an application where I call the following code when an exception occurs- that is, it is located within the "catch" of a "try/catch". It merely creates a "popup"-like window which tells the user to enter an int instead.

Stage dialogStage = new Stage();
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.setScene(new Scene(VBoxBuilder.create().
children(new Text("Please enter an integer."), new Button("Ok.")).
alignment(Pos.CENTER).padding(new Insets(5)).build()));
dialogStage.show();

The only problem is that the stage lingers even if the primary stage (that is the parameter in the public void start(Stage primaryStage)) is closed.

What have I tried?:

I made the dialogstage visible in the whole class by defining it as a class variable. Then I implemented the following code:

primaryStage.setOnCloseRequest((new EventHandler<WindowEvent>(){

        @Override
        public void handle(WindowEvent arg0) {
                            arg0.consume();
            try
            {
             dialogStage.close();

                            }
            catch(Exception ex)
            {
                System.out.print(ex.getMessage()+"\r\n");
            }

        }
    }));
}

This "works" in that the dialogstage is closed when a user tries to exit the main application, however, it does not actually do what I intended: when you try to close the primary stage only the dialogstage is closed.

It is also an aesthetically displeasing solution as I don't want the whole class to know about the stupid dialog box which is only located in one try/catch and might never be used.

I'd like to know if there is a simple solution that tells the dialogstage to close when the primary stage does and where I don't have to write much code outside of my try/catch.

Best Answer

On the event OnCloseRequest on your main stage, perform Platform.exit (docs.oracle.com/javafx/2/api/javafx/application/Platform.html)

Related Topic