Beans Event Listeners
Event Listeners
-
In the JDK 1.0 event model discussion, we described how you could subclass
an object and implement handleEvent() to catch events. We also explained
how events were propagated up to superclasses and also up the component
chain from children to their parents.
-
The listener event model is a bit different. Instead of events automatically
posting to components, their parents, and their superclasses, you must
explicitly request an event to receive it.
-
So, events aren't posted to all sorts of methods that determine if they
want to handle them. Instead they are only posted to interested parties.
-
In the listener event model, an object that wants to listen for events
explicitly notifies the object that generates them that it's interested
in receiving those events. The object will add the listener to a list of
listeners and inform the listener group whenever an event occurs.
Becoming a Listener
-
As we have learned, a button posts an action event when it is pressed.
Thus to listen for a button press, you can inform the button that you are
interested in receiving button press events. To do so, you must call the
addActionListener() method for that button (or whatever component you want
to listen to). Here is a sample call in which "listener" is the name of
the object who will be listening for button events:
button.addActionListener(listener);
This tells the button that when it generates an event, it should notify
the listener of the event.
To inform a listener of an event, the button will call the listener's actionPerformed()
method.
But wait! Where did the actionPerformed() method come from?
To ensure that the target object has an actionPerformed() method, the button
only talks with objects that implement the ActionListener interface. The
ActionListener interface looks like this:
public interface ActionListener
extends EventListener
{
public void actionPerformed(ActionEvent e);
}
Oh, by the way, the listener interfaces and event
classes are contained in the java.awt.event package. Thus to use these
classes you should import the event package like this:
import java.awt.event.*; |
-
Thus, if you want to be a listener of buttons you must implement the ActionListener
interface that requires that you implement a single method, actionPerformed().
-
Here is a more complete example of creating a dialog that listens for action
events from a push button. Notice that the MyDialog class implements the
ActionListener interface and, as such, defines the actionPerformed() method:
class MyDialog extends Dialog
implement ActionListener
{
private Button _okButton;
public void MyDialog()
{
... initialization code ...
_okButton = new Button("OK");
_okButton.addActionListener(this);
... more initialization code ...
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == _okButton)
System.out.println("OK button was pressed");
}
}
One benefit of the listener model is that it allows an outsider to look
at a component and by looking at its methods, know what events it posts.
For example, if you look at the Component class, you will find a number
of methods to add and remove listeners including:
public void addFocusListener(FocusListener l);
public void addKeyListener(KeyListener l);
public void addMouseListener(MouseListener l);
public void removeFocusListener(FocusListener l);
public void removeKeyListener(KeyListener l);
public void removeMouseListener(MouseListener l);
You can use these methods to listen for focus, key, and mouse events from
any component. To listen for one of these events, add the object that you
want to receive the event as a listener and implement the interface corresponding
to the given event.
The JDK contains a large number of listener interfaces that can be implemented
to listen for different types of events. Consider the following list of
listener classes and the interfaces they define.
Listener |
Methods you must Implement |
ActionListener |
actionperformed() |
AdjustmentListener |
adjustmentValueChanged() |
ComponentListener |
componentResized()
componentMoved()
componentShown()
componentHidden() |
ContainerListener |
componentAdded()
componentRemoved() |
FocusListener |
focusGained()
focusLost() |
ItemListener |
itemStateChanged() |
KeyListener |
keypressed()
keyReleased()
keyTyped() |
MouseListener |
mouseEntered()
mouseExited()
mousePressed()
mouseReleased()
mouseClicked() |
MouseMotionListener |
mouseMoved()
mouseDragged() |
TextListener |
textValueChanged() |
WindowLstener |
windowOpened()
windowClosed()
widonwIconified()
windowDeiconified()
windowActivated()
windowDeactivated() |
Additional Resources:
Beans
Events
Table of Contents
Exercise Nine
|