Kiva
Kiva

Reputation: 9353

Do javafx operations before quit application

I would like to open a window with a progress bar when I quit my application while some operations run.

I tried to put my code in the stop method from the Application class but javafx is already down at this moment.

I did the same thing in the first windows like this:

primaryStage.setOnCloseRequest(event -> {
    Stage loading = new Stage();
    loading.initModality(Modality.WINDOW_MODAL);
    loading.initOwner(primaryStage);
    loading.setScene(new Scene(new Group(JfxUtils.loadFxml(new FXMLLoader(), FxmlFileConstantes.LOADING))));
    loading.show();
});

But same thing, it doesn't work.

How can I do this ?

Thanks.

Upvotes: 1

Views: 112

Answers (1)

jewelsea
jewelsea

Reputation: 159416

Consume the close request event in your onCloseRequest event handler.

event.consume();

This will stop the window from closing, which, if the window is the last window in the application, would trigger an application shutdown.

Alternately, you can set Platform.setImplicitExit(false), in which case "the application will continue to run normally even after the last window is closed, until the application calls exit()".

The JavaFX application lifecycle is defined in the javadoc of the Application class, which you should probably read to get some more background information.

Upvotes: 1

Related Questions