Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close all stages when the primary stage is closed?

Tags:

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.

like image 347
The Unfun Cat Avatar asked Aug 30 '12 10:08

The Unfun Cat


People also ask

How to close primary stage in JavaFX?

Creating a Close button setOnAction( e -> primaryStage. close() ); In this case, the action event handler simply calls primaryStage. close() to close the application.

How to close a JavaFX window?

@ZinMinn if you want to close the app, just call Platform. exit(). Closing a stage is different than closing the app (you can have multiple stages). This solution is perfect to close a secondary window.

How do you set the title of the stage primary stage?

We use Stage primStage = (Stage) input. getScene(). getWindow(); within the doStuff() method to get a handle on the primary stage. That solution is the simplest, the current accepted solution might be overkill in a lot of cases.


1 Answers

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

like image 177
Teocali Avatar answered Dec 25 '22 13:12

Teocali