我想在关闭JavaFX应用程序之前保存文件.
这就是我在以下方式设置处理程序的方式Main::start:
primaryStage.setOnCloseRequest(event -> {
System.out.println("Stage is closing");
// Save file
});
Run Code Online (Sandbox Code Playgroud)
Stage::close当按下按钮时控制器调用:
@FXML
public void exitApplication(ActionEvent event) {
((Stage)rootPane.getScene().getWindow()).close();
}
Run Code Online (Sandbox Code Playgroud)
如果我关闭窗口,单击窗口边框上的红色X(正常方式),然后我得到输出消息" Stage is closing",这是所需的行为.
但是,在调用Controller::exitApplication应用程序时关闭而不调用处理程序(没有输出).
如何让控制器使用我添加的处理程序primaryStage?