COM 3118
Date: April 16, 2001
Event Handling
Event Delegation Model
Defines standard and consistent mechanism to generate and process events.
Concept:
A source generates an event and sends it to one or more listeners.
Listener simply waits until it receives an event.
Listener processes events and then returns.
User interface element is able to "delegate" the processing of an event to a separate piece of code.
Notifications are sent only to those listeners that want to receive them.
Old Approach (Java 1.1)
Event was propagated up the containment hierarchy until it was handled by a component.
Event
In the delegation model, an event is an object that describes a state change in a source.
Event Source
A source is an object that generates an event. This occurs when object changes in some way.
Source may generate more than one type of event.
A source must register listeners in order for the listeners to receive notifications about a specific type of event.
Each type of event has its own registration method.
General form:
public void addTypeListener (TypeListener el)
When an event occurs, all registered listeners are notified and receive a copy of the event object.
It is called Multicasting the event.
Some sources may allow only one listener to register.
General form:
public void addTypeListener (TypeListener el) throws java.util.TooManyListenersException
To remove Listener
Public void removeTypeListener (TypeListener el)
Event Listener
A listener is an object that is notified when an event occurs.
Event Classes
At root of Java event class hierarchy is EventObject, which is in java.util.
EventObject (Object src)
Contains 2 methods:
Object getSource()
String toString()
AWTEvent class defined in java.awt package is a subclass of EventObject.
It is superclass (directly or indirectly) of all AWT-based events handled by delegation model.
Method getID () can be used to determine the type of event.
Main Event Classes in java.awt.event
Event Class ActionEvent
AdjustmentEvent ComponentEvent
ContainerEvent FocusEvent InputEvent ItemEvent KeyEvent
MouseEvent TextEvent
WindowEvent |
Description Generated when a button is pressed, a list is double-clicked, or a menu item is selected. Generated when a scroll bar is manipulated. Generated when a component is hidden, moved, resized, or becomes visible. Generated when a component is added to or removed from a container. Generated when a component gains or loses keyboard focus. Abstract super class for all component input event classes. Generated when a check box or a list item is clicked; also occurs when a choice selection is made or a checkable menu is selected or deselected. Generated when input is received from the keyboard. Generated when the mouse is dragged, moved, clicked, pressed, or released; also generated when the mouse enters or exits a component. Generated when the value of a textarea or textfield is changed. Generated when a window os activated, closed, deactivated, deiconified, iconified, opened, or quit. |
Sources of Events
Event Source
Button
Checkbox
Choice
List
Menu Item
Scrollbar
Text Components
Window
Event Listener
Methods in Interfaces:
ActionListener
void actionPerformed (ActionEvent ae)
AdjustmentListener
void adjustmentValueChanged (AdjustmentEvent ae)
ComponentListener
void componentResized (ComponentEvent ce)
void componentMoved (ComponentEvent ce)
void componentShown (ComponentEvent ce)
void componentHidden (ComponentEvent ce)
ContainerListener
void componentAdded (ContainerEvent ce)
void componentRemoved (ContainerEvent ce)
FocusListener
void focusGained (FocusEvent fe)
void focusLost (FocusEvent fe)
ItemListener
void itemStateChanged (ItemEvent ie)
KeyListener
void keyPressed (KeyEvent ke)
void keyReleased (KeyEvent ke)
void keyTyped (KeyEvent ke)
MouseListener
void mouseClicked (MouseEvent me)
void mouseEntered (MouseEvent me)
void mouseExited (MouseEvent me)
void mousePressed (MouseEvent me)
void mouseReleased (MouseEvent me)
MouseMotionListener
void mouseDragged (MouseEvent me)
void mouseMoved (MouseEvent me)
TextListener
void textChanged (TextEvent te)
WindowListener
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)
Adapter Class
Provides an empty implementation of all methods in an event listener interface.
Useful when u want to listen and process only some of the events that are handled by one particular event listener interface.
Define your own class as subclass of this class and provide desired implementation.
Listener Interfaces implemented by Adapter classes
Adapter Class ComponentAdapter ContainerAdapter FocusAdapter KeyAdapter MouseAdapter MouseMotionAdapter WindowAdapter |
Listener Interface ComponentListener ContainerListener FocusListener KeyListener MouseListener MouseMotionListener WindowListener |
Sample Code
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="AdapterDemo" width=300 height=100>
</applet>
*/
public class AdapterDemo extends Applet {
public void init () {
addMouseListener(new MyMouseAdapter (this));
addMouseMotionListener(new MyMouseMotionAdapter (this));
}
}
class MyMouseAdapter extends MouseAdapter {
AdapterDemo adapterDemo;
public MyMouseAdapter(AdapterDemo adapterDemo) {
this.adapterDemo = adapterDemo;
}
// Handle mouse clicked
public void mouseClicked (MouseEvent me) {
adapterDemo.showStatus ("Mouse Clicked");
}
}
class MyMouseMotionAdapter extends MouseMotionAdapter {
AdapterDemo adapterDemo;
public MyMouseMotionAdapter (AdapterDemo adapterDemo) {
this. adapterDemo = adapterDemo;
}
// Handle Mouse Drag
public void mouseDragged (MouseEvent me) {
adapterDemo.showStatus("Mouse Dragged");
}
}
Inner Class
Normal Case
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="MousePressedDemo" width=300 height=100>
</applet>
*/
public class MousePressedDemo extends Applet {
public void init () {
addMouseListener(new MyMouseAdapter (this));
}
}
class MyMouseAdapter extends MouseAdapter {
MousePressedDemo mousePressedDemo;
public MyMouseAdapter (MousePressedDemo mousePressedDemo) {
this.mousePressedDemo = mousePressedDemo;
}
// Handle mouse clicked
public void mouseClicked (MouseEvent me) {
mousePressedDemo.showStatus ("Mouse Clicked");
}
}
Modified Version
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="InnerClassDemo" width=300 height=100>
</applet>
*/
public class InnerClassDemo extends Applet {
public void init () {
addMouseListener(new MyMouseAdapter ());
}
class MyMouseAdapter extends MouseAdapter {
public void mouseClicked (MouseEvent me) {
showStatus ("Mouse Clicked");
}
}
}
Anonymous Inner Classes
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code=" AnonymousInnerClassDemo" width=300 height=100>
</applet>
*/
public class AnonymousInnerClassDemo extends Applet {
public void init () {
addMouseListener(new MyMouseAdapter () {
public void mouseClicked (MouseEvent me) {
showStatus ("Mouse Clicked");
}
});
}
}