I want to save a file before closing my JavaFX application.
This is how I'm setting up the handler in Main::start
:
primaryStage.setOnCloseRequest(event -> {
System.out.println("Stage is closing");
// Save file
});
And the controller calling Stage::close
when a button is pressed:
@FXML
public void exitApplication(ActionEvent event) {
((Stage)rootPane.getScene().getWindow()).close();
}
If I close the window clicking the red X on the window border (the normal way) then I get the output message "Stage is closing
", which is the desired behavior.
However, when calling Controller::exitApplication
the application closes without invoking the handler (there's no output).
How can I make the controller use the handler I've added to primaryStage
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…