The Delegation Event Model
- The modern approach to handling events is based on the delegation event model, which defines standard and consistent mechanisms to generate and process events.
- Its concept is quite simple: a source generates an event and sends it to one or more listeners.
- In the scheme, the listener simply waits until it receives an event. Once received, the listener processes event and then returns. The advantage of this design is that the application logic that processes events is clearly separated from the user interface logic that generates those events.
- In the delegation event model, listeners must register with a source in order to receive an event notification. This provides an important benefit: notifications are sent only to listeners that want to receive them.
Event:
- An event is an object that describes some change in source. It can be generated when a person interacts with an element in a GUI. For example pressing a button, clicking a mouse, double clicking on a list box entry or closing window.
- Events may also occur that are not directly caused by interactions with a user interface. For example, an event may be generated when a time expires, counter exceeds a value, a software or hardware failure occurs.
Event Source:
A source generates events. It has three main responsibilities
- It must provide methods that allow listeners to register and unregister for notifications about a specific type of events.
- It must generate the event
- It must send the event to all registered listeners. The event may be a unicast to a single listeners or multicast to several listeners. It is possible for a source to generate several types of events.
- Each type of event has its own registration method. Here is the general form:
public void addTypeListener(TypeListener el)
Event Listeners:
A listener receives event notifications. It has three main responsibilities
- It must register to receive notifications account specific events. It does so by calling the appropriate registration method of source.
- It must implement an interface to receive events of that type.
- It must unregister if it is no longer to proceed those notifications. It does so by calling appropriate unregistration method of the source.
Event Classes
The classes that represent events are at the core of Java’s event handling mechanism.
Object
Event Object
AWT Event
ActionEvent AdjustmentEvent ComponentEvent ItemEvent TextEvent
ContainerEvent FocusEvent InputEvent WindowEvent
KeyEvent MouseEvent
At the root of the Java event class hierarchy is EventObject, which is in java.util.
It is the superclass for all events. Its one constructor is shown here:
EventObject(Object src). Here, src is the object that generates this event.
|
Method |
Description |
|
Object getSource( ) |
Returns the object that generated the event |
|
String toString( ) |
Returns the string equivalent of the event |
To summarize:
■ EventObject is a superclass of all events.
■ AWTEvent is a superclass of all AWT events that are handled by the delegation
event model.
The package java.awt.event defines several types of events that are generated by
various user interface elements
Main Event Classes in java.awt.event
|
Event Class |
Generated When |
|
ActionEvent |
Generated when a button is pressed, a list item is double-clicked, or a menu item is selected. |
|
AdjustmentEvent |
Generated when a scroll bar is manipulated. |
|
ComponentEvent |
Generated when a component is hidden, moved, resized, or becomes visible. |
|
ContainerEvent |
Generated when a component is added to or removed from a container. |
|
FocusEvent |
Generated when a component gains or loses keyboard focus. |
|
InputEvent |
A mouse or key event occurs. |
|
ItemEvent |
Generated when a check box or list item is clicked; also occurs when a choice selection is made or a checkable menu item is selected or deselected. |
|
KeyEvent |
Generated when input is received from the keyboard. |
|
MouseEvent |
Generated when the mouse is dragged, moved, clicked,pressed, or released; also generated when the mouse enters or exits a component. |
|
MouseWheelEvent |
Generated when the mouse wheel is moved. |
|
TextEvent |
Generated when the value of a text area or text field is changed. |
|
WindowEvent |
Generated when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit. |
Sources of Events
The table lists some of the user interface components that can generate the events
|
Event Source |
Description |
|
Button |
Generates action events when the button is pressed. |
|
Checkbox |
Generates item events when the check box is selected or deselected. |
|
Choice |
Generates item events when the choice is changed. |
|
List |
Generates action events when an item is double-clicked; generates item events when an item is selected or deselected. |
|
Menu Item |
Generates action events when a menu item is selected; generates item events when a checkable menu item is selected or deselected. |
|
Scrollbar |
Generates adjustment events when the scroll bar is manipulated. |
|
Text components |
Generates text events when the user enters a character. |
|
Window |
Generates window events when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit. |
Event Listener Interfaces
The delegation event model has two parts: sources and listeners. Listeners are created by implementing one or more of the interfaces defined by the java.awt.event package. When an event occurs, the event source invokes the appropriate method defined by the listener and provides an event object as its argument. Table lists commonly used listener interfaces and provides a brief description of the methods that they define.
|
Interface |
Description |
Methods |
|
ActionListener |
Defines one method to receive action events. |
void actionPerformed(ActionEvent ae) |
|
AdjustmentListener |
Defines one method to receive adjustment events. |
void adjustmentValueChanged(AdjustmentEvent ae) |
|
ComponentListener |
Defines four methods to recognize when a component is hidden, moved, resized, or shown. |
void componentResized(ComponentEvent ce) void componentMoved(ComponentEvent ce) void componentShown(ComponentEvent ce) void componentHidden(ComponentEvent ce) |
|
ContainerListener |
Defines two methods to recognize when a component is added to or removed from a container. |
void componentAdded(ContainerEvent ce) void componentRemoved(ContainerEvent ce) |
|
FocusListener |
Defines two methods to recognize when a component gains or loses keyboard focus. |
void focusGained(FocusEvent fe) void focusLost(FocusEvent fe) |
|
ItemListener |
Defines one method to recognize when the state of an item changes. |
void itemStateChanged(ItemEvent ie) |
|
KeyListener |
Defines three methods to recognize when a key is pressed, released, or typed. |
void keyPressed(KeyEvent ke) void keyReleased(KeyEvent ke) void keyTyped(KeyEvent ke) |
|
MouseListener |
Defines five methods to recognize when the mouse is clicked, enters a component, exits a component, is pressed, or is released. |
void mouseClicked(MouseEvent me) void mouseEntered(MouseEvent me) void mouseExited(MouseEvent me) void mousePressed(MouseEvent me) void mouseReleased(MouseEvent me) |
|
MouseMotionListener |
Defines two methods to recognize when the mouse is dragged or moved. |
void mouseDragged(MouseEvent me) void mouseMoved(MouseEvent me) |
|
TextListener |
Defines one method to recognize when a text value changes. |
void textChanged(TextEvent te) |
|
WindowFocusListener |
Defines two methods to recognize when a window gains or loses input focus. |
void windowGainedFocus(WindowEvent we) void windowLostFocus(WindowEvent we) |
|
WindowListener |
Defines seven methods to recognize when a window is activated, closed, deactivated, deiconified, iconified, opened, |
void windowActivated(WindowEvent we) void windowClosed(WindowEvent we) void windowClosing(WindowEvent we) void windowDeactivated(WindowEvent we) void windowDeiconified(WindowEvent we) void windowIconified(WindowEvent we) void windowOpened(WindowEvent we) |
// Demonstrate the mouse event handlers.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MouseEventEx extends JFrame implements MouseListener
{
Container cp;
public MouseEventEx()
{
cp=getContentPane();
addMouseListener(this);
setSize(300,300);
setTitle(“Mouse Listener”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
cp.setBackground(Color.pink);
System.out.println(e.getX()+”,”+e.getY());
System.out.println(“mouseClicked”);
}
public void mouseEntered(MouseEvent e)
{
cp.setBackground(Color.yellow);
System.out.println(“mouseEntered”);
}
public void mouseExited(MouseEvent e)
{
cp.setBackground(Color.cyan);
System.out.println(“mouseExited”);
}
public void mousePressed(MouseEvent e)
{
cp.setBackground(Color.red);
System.out.println(“mousePressed”);
}
public void mouseReleased(MouseEvent e)
{
cp.setBackground(Color.blue);
System.out.println(“mouseReleased”);
}
public static void main(String[] args)
{
new MouseEventEx();
}
}
// Demonstrate the key event handlers.
import java.awt.*;
import java.awt.event.*;
public class SimpleKey extends JFrame implements KeyListener {
String msg = “”;
int X = 10, Y = 20; // output coordinates
public SimpleKey () {
addKeyListener(this);
requestFocus(); // request input focus
setSize(300,300);
setTitle(“Mouse Listener”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void keyPressed(KeyEvent ke) {
showStatus(“Key Down”);
}
public void keyReleased(KeyEvent ke) {
showStatus(“Key Up”);
}
public void keyTyped(KeyEvent ke) {
msg += ke.getKeyChar();
repaint();
}
// Display keystrokes.
public void paint(Graphics g) {
g.drawString(msg, X, Y);
}
public static void main(String[] args)
{
new SimpleKey ();
}
}
JAVA LIBRARY