Events
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
(Xyz
Listener), a class that provides event information
(Xyz
Event), and an API method to add the listener
(add
Xyz
Listener). If there is more
than one method defined in the listener interface then there is an adapter
(Xyz
Adapter) that implements the listener interface
and provides empty methods. All of the events, listeners,
and adapters are defined in the package
org.eclipse.swt.events
.
The following tables summarize the events that are available and the widgets
that support each event. Events can be split into two general categories:
high level events which represent a logical operation on a control, and low
level events which describe more specific user interactions. High level events
may be represented by multiple low level events which may differ per
platform. Low level events should generally only be used for custom widget
implementations.
High level events
Event Type
|
Description
|
Activate, Deactivate
|
Generated when a Control is activated or deactivated. |
Arm
|
A MenuItem is armed (highlighted and ready to be selected). |
Close
|
A Shell is about to close as requested by the window manager. |
DefaultSelection
|
The user selects an item by invoking a default selection action. For
example, by hitting Enter or double clicking on a row in a Table. |
Dispose
|
A widget is about to be disposed, either programmatically or by user. |
DragDetect
|
The user has initiated a possible drag operation. |
EraseItem
|
A TableItem or TreeItem is about to have its background drawn. |
Expand, Collapse
|
An item in a Tree is expanded or collapsed. |
Help
|
The user has requested help for a widget. For example, this occurs when
the F1 key is pressed under Windows. |
Iconify, Deiconify
|
A Shell has been minimized, maximized, or restored. |
ImeComposition
|
Allows custom text editors to implement in-line editing of international text. |
MeasureItem
|
The size of a custom drawn TableItem or TreeItem is being requested. |
MenuDetect
|
The user has requested a context menu. |
Modify
|
The widget's text has been modified. |
Move, Resize
|
A control has changed position or has been resized, either programmatically
or by user. |
Movement
|
An updated caret offset is needed in response to a user action in a StyledText. |
PaintItem
|
A TableItem or TreeItem is about to have its foreground drawn. |
Selection
|
The user selects an item in the control. For example, by single clicking
on a row in a Table or by keyboard navigating through the items. |
SetData
|
Data needs to be set on a TableItem when using a virtual table. |
Settings
|
An operating system property, such as a system font or color, has been changed. |
Show, Hide
|
A control's visibility has changed. |
Traverse
|
The user is trying to traverse out of the control using a keystroke. For
example, the escape or tab keys are used for traversal. |
Verify
|
A widget's text is about to be modified. This event gives the application
a chance to alter the text or prevent the modification. |
Low level events
Event Type
|
Description
|
FocusIn, FocusOut
|
A control has gained or lost focus. |
KeyDown, KeyUp
|
The user has pressed or released a keyboard key when the control has
keyboard focus. |
MouseDown, MouseUp,
MouseDoubleClick
|
The user has pressed, released, or double clicked the mouse over the
control. |
MouseMove
|
The user has moved the mouse above the control. |
MouseEnter, MouseExit,
MouseHover
|
The mouse has entered, exited, or hovered over the control. |
MouseWheel
|
The mouse wheel has been rotated. |
Paint
|
The control has been damaged and requires repainting. |
Untyped events
In addition to the typed event system described above, SWT supports a low level,
untyped widget event mechanism. 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);