When exiting a JavaFX program I'm overriding Application.stop() in order to check for unsaved changes. This works okay, but it would be nice to give the user the option to cancel the operation.
Prevent or cancel exit JavaFX 2
javafx-2window
javafx-2window
When exiting a JavaFX program I'm overriding Application.stop() in order to check for unsaved changes. This works okay, but it would be nice to give the user the option to cancel the operation.
Best Answer
Application.stop() is last-chance-saloon in other words although it does trap the exit, it's a bit late to revoke the exit process.
Better is to set a listener for the close request which can be cancelled by consuming the event.
In the application class:
and then in the application controller, referenced as
appCtrl
above:noting: mainAppPane is referenced in the FXML ( using the JavaFX Scene Builder in this case ) to allow access to the scene and window; the dialog is one extended from https://github.com/4ntoine/JavaFxDialog and fileSave is the event handler for File -> Save menu item. For the File -> Exit menu item:
Hope this helps someone!