我有用于Scene.fxml的Controller.java和用于WindowSettings.fxml的ControllerSettings.java。在Controller.java中,我使用以下方法创建一个新的弹出窗口(无对话框):
@FXML
public void handleSubmenuSettings(ActionEvent event) throws IOException {
Stage stage;
Parent root;
ControllerSettings controller;
stage = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("WindowSettings.fxml"));
root = (Parent) loader.load();
controller = (ControllerSettings) loader.getController();
stage.setScene(new Scene(root));
stage.setTitle("Settings");
stage.initModality(Modality.APPLICATION_MODAL);
stage.setResizable(false);
stage.initOwner(submenuSettings.getScene().getWindow());
stage.showAndWait();
stage.setOnCloseRequest(e -> {
e.consume();
controller.saveSettings();
stage.close();
});
}
我想在关闭新的弹出窗口时保存设置,但这不适用于stage.setOnCloseRequest。
答案 0 :(得分:0)
showAndWait()
方法将阻止执行,直到窗口关闭为止;即,在窗口关闭之后才会执行后续声明。因此,在窗口关闭之前,您不会为关闭请求注册监听器。显然,在窗口关闭后,不会发生关闭窗口的请求,因此永远不会调用您的处理程序。
不清楚为什么要使用关闭请求处理程序。大概你只想在窗口关闭后调用controller.saveSettings()
。由于您使用showAndWait()
,您可以这样做:
@FXML
public void handleSubmenuSettings(ActionEvent event) throws IOException {
Stage stage;
Parent root;
ControllerSettings controller;
stage = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("WindowSettings.fxml"));
root = (Parent) loader.load();
controller = (ControllerSettings) loader.getController();
stage.setScene(new Scene(root));
stage.setTitle("Settings");
stage.initModality(Modality.APPLICATION_MODAL);
stage.setResizable(false);
stage.initOwner(submenuSettings.getScene().getWindow());
stage.showAndWait();
controller.saveSettings();
}