java 在 GWT 中关闭窗口时执行代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2562658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:48:39  来源:igfitidea点击:

Execute code on window close in GWT

javaeventsgwt

提问by stuff22

I'd like to do something like this:

我想做这样的事情:

Window.addWindowClosingHandler(new Window.ClosingHandler() {

    @Override
    public void onWindowClosing(ClosingEvent event) {
        event.setMessage("Really?");

        // If user clicks 'ok' in the dialog, execute code below. Else skip the code and return to window.

        // CODE that does stuff goes here.
    }
});

How do I capture the input from the dialog?

如何从对话框中捕获输入?

采纳答案by stuff22

There need to be two handlers, one Window.ClosingHandlerand one CloseHandler. See below. This will make sure, if 'cancel' is clicked in the dialog, the CloseHandlerisn't triggered. But if 'ok' is clicked, the CloseHandleris executed and will run the necessary code. This could be used for releasing db locks, neatly closing open sessions, etc.

需要有两个处理程序,一个Window.ClosingHandler和一个CloseHandler。见下文。这将确保,如果在对话框中单击“取消”,CloseHandler则不会触发。但是,如果单击“确定”,CloseHandler则会执行并运行必要的代码。这可以用于释放数据库锁,巧妙地关闭打开的会话等。

Window.addWindowClosingHandler(new Window.ClosingHandler() {

    @Override
    public void onWindowClosing(ClosingEvent event) {
        event.setMessage("You sure?");
    }
});

Window.addCloseHandler(new CloseHandler<Window>() {

    @Override
    public void onClose(CloseEvent<Window> event) {
        //Execute code when window closes!
    }
});

回答by NebuSoft

You want to look into Window.Confirm for this kind of functionality.

您想查看 Window.Confirm 以获得此类功能。

You can read up on it here: gwt.user.client.Window

你可以在这里阅读它: gwt.user.client.Window