Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

CHPT5.

EVENT
5. EVENT
Many desktop applications interact with the user through event mechanisms, such as
a mouse or keyboard being pressed or released, Event uses listener to notify the
application which component has a certain action, and then reacts accordingly.
The org.eclipse.swt.events package contains interfaces for all SWT-specific event
classes and listeners.

Event: The message sent to the GUI system that informs the GUI system that
something has happened and requires a response.
Event source: The component object that raised the event.
Event processing: Java GUI system executes a specific method or runs a specific
program for some operations on the component, so that the user can exchange data
with the application, or control the process of the program
5.1 EVENT PROCESSING
MODEL
Java 2 (JDK1.1) and later versions use the delegated event model to monitor and
process events that occur on components.

click
运行时环境
SelectionEvent e

Composite

窗口
button
button
Delegation: event processing Event listener

widgetSelected (SelectionEvent e) { }
5.2 EVENT PROCESSING MODE
(1) Define an XxxEvent that describes the Xxx event of the GUI.
(2) Define an event handler interface XxxListener, and declare all the processing
methods related to the event.
(3) Define the registration method addXxxListener and the logout method
removeXxxListener for processing Xxx events in the component that triggers the
event.
(4) Write a class that implements the event listener interface, and implement
specific event processing methods.
5.3.1 LISTENER
The listener (the object that receives the event) is responsible for processing the
code that responds to the event after the event arrives.
Listener object is an interface that implements methods in the interface to
respond to an event. The form of listener classes are XXXListener.
Example: SelectionListener, MouseListener, and KeyListener.

There are different ways of responding to events within a type of listener. For
example, two methods (widgetSelected and widgetDefaultSelected) can be
implemented in SelectionListener.
5.3.1 LISTENER
In SWT, there is a corresponding Listener class for each event. If an event is X, the
corresponding Listener class is XListener, and its add listener method is
addXListener.

Example:
button.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {...}
public void widgetDefaultSelected(SelectionEvent e) {...}
});
click
Here's the code
5.3.2 ADAPTER
The SWT class library has designed a corresponding event adapter class for
the event listener interface with two or more methods, and implemented for
each abstract method.
The event listener of the SWT application can be derived from the
corresponding event adapter class, and only the required methods are
overwritten in the event listener class.

The general idea of using adapters is to facilitate programmers to write


processing code. If more than one method are defined in a listener interface,
an adapter (xaadapter) will be provided accordingly.
5.3.2 ADAPTER
Example:

button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {...}
});

click
Here's the code
5.3.3 UNTYPE EVENT
MECHANISM
SWT provides an untyped (UnType) listener, which knows the event by calling its
handleEvent() method (HandleEvent() is a method of the Listener class), which can handle
any event. Use addListener (int eventType, Listener listener) to add a listener to a listener
collection. When an event of the specified type is triggered, the listener will know the event
through handleEvent().
Example:
SWT.KeyDown: Triggered when the user presses the key
SWT.KeyUp: Triggered when the user releases the key
SWT.MenuDetect: Triggered when the menu is selected
SWT.Resize: Triggered when the widget is resized
...
5.3.3 UNTYPE EVENT
MECHANISM
Example:
Listener listener = new Listener() {
public void handleEvent(Event e) {
switch (e.type) {
case SWT.MouseDown:
...;
case ...
}
}
}; Here's the code
shell.addListener(SWT.MouseDown, listener);
5.4 METHODS OF EVENT
PROCESSING
Way 1: Anonymous Inner Class.
Anonymous inner class is the simplest method, which is suitable for some
simple events.
Example:
list.addSelectionListener(
new SelectionListener(){
});

Disadvantage: the code cannot be reused. Once registered, the listener registered
in this method is not easy to remove.
5.4 METHODS OF EVENT
PROCESSING
Way 2: Inner class.
Example:
SelectionListener listener = new SelectionListener(){
public void widgetSelected(SelectionEvent e){...}
public void widgetDefaultSelected(){...}
}
list.addSelectionListener(listener );
list.removeSelectionListener(listener);

Advantage: it can register the listener for the control, and remove it when
necessary, and it can also register the same listener for multiple controls.
5.4 METHODS OF EVENT
PROCESSING
Way 3: Independent listener class.
The event listener class can be defined as an independent class, which is not inside any
class. It can create listeners for components in the package or any Java SWT GUI class.
Example:
class MyListener{
public void mouseDoubleClick(MouseEvent e) {...);
}
The listener is only applied to one component --- Anonymous inner class.
The listener class is to be used by multiple components in the same interface --- inner
class.
Different interfaces or classes can use the same listener class --- independent listener class.
5.5 KEYEVENT
KeyEvent: Triggered when a key is pressed or released.
Event Listener Adapter function
KeyEvent KeyListener KeyAdaper keyPressed()
keyReleased()

Untyped Discription
Event
SWT.KeyDo Triggered when the user presses the key
wn
SWT.KeyUp Triggered when the user releases the key

public void keyPressed(KeyEvent e);


public void keyReleased(KeyEvent e);
5.5 KEYEVENT
Example:
public void cancelKeyInput() {
text.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {...}
});
}
before: input “abc” after: input “abc”

Here's the code click


button
5.6 MOUSEEVENT
MouseEvent: It include many operations, like mouse button pressing, releasing,
double clicking and mouse cursor moving or above the GUI.

MouseListener Interface

Three methods :
public void mouseDoubleClick(MouseEvent e);
public void mouseDown(MouseEvent e);
public void mouseUp(MouseEvent e);
5.6 MOUSEEVENT
When the mouse button is pressed, released or double clicked on the defined shell
form, or when the mouse cursor enters, exits the shell and moves on the form, the
mouse trigger state and coordinates on the shell will be output.
Example:
shlMouselistener.addMouseListener(new MouseListener() { before
public void mouseDown(MouseEvent e) {...}
public void mouseUp(MouseEvent e) {...}
public void mouseDoubleClick(MouseEvent arg0) {...} after

});

Here's the code


Any Question ?

You might also like