Once we create a display and some widgets, and start up the application's message loop, where does the real work happen? It happens every time an event is read from the queue and dispatched to a widget. Most of the application logic is implemented as responses to user events.
The basic pattern is that you add a listener to some widget that you have created, and when the appropriate event occurs the listener code will be executed. This simple example is adapted from org.eclipse.swt.examples.helloworld.HelloWorld3:
Display display = new Display (); Shell shell = new Shell (display); Label label = new Label (shell, SWT.CENTER); ... shell.addControlListener (new ControlAdapter () { public void controlResized (ControlEvent e) { label.setBounds (shell.getClientArea ()); } });
For each type of listener, there is an interface that defines the listener (XyzListener), a class that provides event information (XyzEvent), and an API method to add the listener (addXyzListener). If there is more than one method defined in the listener interface then an adapter (XyzAdapter) that implements the listener interface and provides empty methods is provided as well. All of the events, listeners, and adapters are defined in the package org.eclipse.swt.events.
The following table summarizes the events that are available and the widgets that support each event.
Event Type |
Description |
Widgets |
---|---|---|
Arm |
Generated when a widget, such as a menu item, is armed. |
MenuItem |
Control |
Generated when a control is moved or resized. |
Control, TableColumn, Tracker |
Dispose |
Generated when a widget is disposed, either programmatically or by the user. |
Widget |
Focus |
Generated when a control gains or loses focus. |
Control |
Help |
Generated when the user requests help for a widget, such as pressing the F1 key. |
Control, Menu, MenuItem |
Key |
Generated when the user presses or releases a keyboard key when the control has keyboard focus. |
Control |
Menu |
Generated when a menu is hidden or shown. |
Menu |
Modify |
Generated when a widget's text is modified. |
CCombo, Combo, Text, StyledText |
Mouse |
Generated when the user presses, releases, or double clicks the mouse over the control. |
Control |
MouseMove |
Generated as the user moves the mouse across the control. |
Control |
MouseTrack |
Generated when the mouse enters, exits, or hovers over the control. |
Control |
Paint |
Generated when the control needs to be repainted. |
Control |
Selection |
Generated when the user selects an item in the control. |
Button, CCombo, Combo, CoolItem, CTabFolder, List, MenuItem, Sash, Scale, ScrollBar, Slider, StyledText, TabFolder, Table, Table Cursor, TableColumn, TableTree, Text, ToolItem, Tree |
Shell |
Generated when a shell is minimized, maximized, activated, deactivated, or closed. |
Shell |
Traverse |
Generated when a control is traversed by the user using keystrokes. |
Control |
Tree |
Generated when the user expands or collapses items in the tree. |
Tree, TableTree |
Verify |
Generated when a widget's text is about to be modified. Gives the application a chance to alter the text or prevent the modification. |
Text, StyledText |
The typed event system described above is implemented with a low level, untyped widget event mechanism. This mechanism is not intended to be used by applications, but you will see it used inside of the SWT implementation. It is also used in many of the workbench wizard page implementations.
The untyped mechanism relies on a constant to identify the event type and defines a generic listener that is supplied with this constant. This allows the listener to implement a "case style" listener. In the following snippet, we define a generic event handler and add several listeners to a shell.
Shell shell = new Shell (); Listener listener = new Listener () { public void handleEvent (Event e) { switch (e.type) { case SWT.Resize: System.out.println ("Resize received"); break; case SWT.Paint: System.out.println ("Paint received"); break; default: System.out.println ("Unknown event received"); } } }; shell.addListener (SWT.Resize, listener); shell.addListener (SWT.Paint, listener);