Execute code on window close in GWT

15,983

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

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!
    }
});
Share:
15,983
stuff22
Author by

stuff22

Updated on June 12, 2022

Comments

  • stuff22
    stuff22 almost 2 years

    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?