阻止或取消退出JavaFX 2 [英] Prevent or cancel exit JavaFX 2

查看:69
本文介绍了阻止或取消退出JavaFX 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

退出JavaFX程序时,我将重写Application.stop()以便检查未保存的更改.可以,但是最好为用户提供取消操作的选项.

解决方案

Application.stop()是last-chance-saloon,尽管它确实会捕获出口,但是撤消出口过程有点晚. >

更好的方法是为关闭请求设置一个侦听器,该关闭请求可以通过使用事件来取消.

在应用程序类中:

public void start(Stage stage) throws Exception {
    FXMLLoader ldr = new FXMLLoader(getClass()
                .getResource("Application.fxml"));
    Parent root = (Parent) ldr.load();
    appCtrl = (ApplicationController) ldr.getController();

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();

    scene.getWindow().setOnCloseRequest(new EventHandler<WindowEvent>() {
        public void handle(WindowEvent ev) {
            if (!appCtrl.shutdown()) {
                ev.consume();
            }
        }
    });
}

,然后在上面引用为appCtrl的应用程序控制器中:

/** reference to the top-level pane                               */
@FXML
private AnchorPane mainAppPane;

public boolean shutdown() {
    if (model.isChanged()) {
        DialogResult userChoice =
                ConfirmDialog.showYesNoCancelDialog("Changes Detected",
                "Do you want to save the changes?  Cancel revokes the "
                + "exit request.",
                mainAppPane.getScene().getWindow());

        if (userChoice == DialogResult.YES) {
            fileSave(null);

            if (model.isChanged()) {
                // cancelled out of the save, so return to the app
                return false;
            }
        }

        return userChoice == DialogResult.NO;
    }

    return true;
}

注意:FXML中引用了mainAppPane(在这种情况下使用JavaFX Scene Builder)以允许访问场景和窗口.该对话框是 https://github.com/4ntoine/JavaFxDialog 的扩展,而fileSave是File-> Save的事件处理程序.菜单项.对于文件->退出菜单项:

@FXML
private void fileExitAction(ActionEvent ev) {
    if (shutdown()) {
        Platform.exit();
    }
}

希望这对某人有帮助!

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.

解决方案

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:

public void start(Stage stage) throws Exception {
    FXMLLoader ldr = new FXMLLoader(getClass()
                .getResource("Application.fxml"));
    Parent root = (Parent) ldr.load();
    appCtrl = (ApplicationController) ldr.getController();

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();

    scene.getWindow().setOnCloseRequest(new EventHandler<WindowEvent>() {
        public void handle(WindowEvent ev) {
            if (!appCtrl.shutdown()) {
                ev.consume();
            }
        }
    });
}

and then in the application controller, referenced as appCtrl above:

/** reference to the top-level pane                               */
@FXML
private AnchorPane mainAppPane;

public boolean shutdown() {
    if (model.isChanged()) {
        DialogResult userChoice =
                ConfirmDialog.showYesNoCancelDialog("Changes Detected",
                "Do you want to save the changes?  Cancel revokes the "
                + "exit request.",
                mainAppPane.getScene().getWindow());

        if (userChoice == DialogResult.YES) {
            fileSave(null);

            if (model.isChanged()) {
                // cancelled out of the save, so return to the app
                return false;
            }
        }

        return userChoice == DialogResult.NO;
    }

    return true;
}

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:

@FXML
private void fileExitAction(ActionEvent ev) {
    if (shutdown()) {
        Platform.exit();
    }
}

Hope this helps someone!

这篇关于阻止或取消退出JavaFX 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆