站内搜索: 请输入搜索关键词
当前页面: 图书首页 > SWT: The Standard Widget Toolkit

5.3 Events and Listeners - SWT: The Standard Widget Toolkit

Previous Section  < Day Day Up >  Next Section

5.3 Events and Listeners

In the same manner as widgets, displays send events using untyped listeners.

addListener(int event, Listener listener) Adds the listener to the collection of listeners that will be notified when an event of the given type occurs. When the event occurs in the display, the listener is notified by calling its handleEvent() method.

removeListener(int event, Listener listener) Removes the listener from the collection of listeners that will be notified when an event of the given type occurs.

The following code fragment adds a listener that will be called when the Display is disposed of.






display.addListener(SWT.Dispose, new Listener() {

    public void handleEvent(Event event) {

        // the display is getting disposed

    }

});


5.3.1 Dispose and Close Listeners

The SWT.Dispose event is sent before the resources for the display and any widgets that were created on it are disposed. This makes a dispose listener a good place to save any data that is global to your application. For example, if your application allows the user to configure the default font to use when creating a certain kind of widget, saving the font name to a file here (to be read back when your program restarts) is a good idea.

In addition to SWT.Dispose, Displays support the SWT.Close event. This event is sent when the user interface session is ending, for example, when the window system is shutting down or the user is logging out. On some platforms, it is possible to cancel this operation by setting the doit field to false.

The following example uses SWT.Dispose and SWT.Close to save both global and window state when either the window is closed or the user interface session is ending.






public static void main(String[] args) {

    Display display = new Display();

    final Shell shell = new Shell(display);

    Listener listener = new Listener() {

        public void handleEvent(Event event) {

            int style =

               SWT.OK | SWT.CANCEL | SWT.APPLICATION_MODAL;

            MessageBox box = new MessageBox(shell, style);

            box.setMessage("Exit the application?");

            event.doit = box.open() == SWT.OK;

        }

    };

    display.addListener(SWT.Close, listener);

    display.addListener(SWT.Dispose, new Listener() {

        public void handleEvent(Event event) {

            System.out.println("Saving global state ... ");

        }

    });

    shell.addListener(SWT.Close, listener);

    shell.addListener(SWT.Dispose, new Listener() {

        public void handleEvent(Event event) {

            System.out.println("Saving window state ... ");

        }

    });

    shell.setSize(200, 200);

    shell.open();

    while (!shell.isDisposed()) {

        if (!display.readAndDispatch())

            display.sleep();

    }

    display.dispose();

}


Note that if you run the above code on a platform that does not support the ability to cancel the shutdown of the user interface session, the application would exit, even if the user presses the Cancel button of the "Exit the application?" dialog. For this reason, it is best not to rely on being able to set the doit flag of the SWT.Close event for the display. Generally speaking, stopping the user interface session from shutting down is not something an application program should do. Some operating systems kill your application if it does not respond within a certain time interval and shut down anyway.

If you would like programmatically to cause the display to behave as though a user interface session were ending, you can call its close() method.

close() Causes the display to send an SWT.Close event to all listeners, then if the event doit flag is still true, dispose of the display.

In addition to listeners and events, Display supports event filters and runnable "execs."

    Previous Section  < Day Day Up >  Next Section