logo

Handling Window Close Events in JFace

logo

Just so I remember how to do this next time… JFace apps work a little different to normal SWT applications. In an SWT app, you’d normally register an event listener and check for SWT.Close. If you do that, you might not get the behaviour you expect (I found that my current shell was gone by the time I got the event!).

JFace actually make all this a whole lot easier. You just need to override handleShellCloseEvent() and do your own magic. The default implementation just closes everything down. In PasswordSafeSWT, I wanted to prompt for the user to save their modified safe before exiting, so my implementation looks like this:

	/**
	 *
	 * @see org.eclipse.jface.window.Window#handleShellCloseEvent()
	 */
	protected void handleShellCloseEvent() {
		boolean cancelled = saveAppIfDirty();
		if (cancelled) {
			setReturnCode(OK);
		} else {
			tidyUpOnExit();
			super.handleShellCloseEvent();
		}
	}

And you’re in business. If the user cancels the exit, you just set a ReturnCode of OK and life continues as normal. If you’re happy that things are ok to exit, just invoke the superclass and you’re done.

This took ages to work out since I was doing all sorts of evil in my SWT.Close event listener to try and work around it. In future I’ll just read the Javadoc.

2 Responses to “Handling Window Close Events in JFace”

  1. niranjan says:

    thanks… you made my work easy.. I was scratching. my head to achive this

  2. Very good. A little diferent from SWT App Component.

    public void setShellEvents(final Shell shell){

    shell.addListener (SWT.Close, new Listener () {
    public void handleEvent (Event event) {
    MessageBox messageBox = new MessageBox (shell,SWT.APPLICATION_MODAL | SWT.YES | SWT.NO);
    messageBox.setText (“Warning”);
    messageBox.setMessage (“You have unsaved data. Close the shell anyway?”);
    if(messageBox.open() == SWT.YES)
    event.doit = true;
    else
    event.doit = false;
    }
    });
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

logo
logo
Powered by WordPress | Designed by Elegant Themes