When the user acts on a Component
--
clicking it or pressing the Return key, for example -- an
Event
is generated.
Events are generated by event sources.
One or more listeners can register to be notified about events of a particular kind from a particular source.
Event handlers can be instances of any class. As long as a class implements an event listener interface.
Code declaring that the class implements a listener interface:
public class MyClass implements ActionListener {
Code that registers an instance of the event handling class as a listener:
someComponent.addActionListener(instanceOfMyClass);
The implementation of the methods in the listener interface:
public void actionPerformed(ActionEvent e) { ...//code that reacts to the action... }
![]() | Note |
---|---|
The AWT doesn't see every event that occurs. The AWT can see only those events that the platform-dependent code lets it see. |
Table 10.1. AWT Events
Listener Interface | Adapter Class | Methods |
---|---|---|
ActionListener | none | actionPerformed |
AdjustmentListener | none | adjustmentValueChanged |
ComponentListener | ComponentAdapter | componentHidden componentMoved
componentResized componentShown |
ContainerListener | ContainerAdapter | componentAdded
componentRemoved |
FocusListener | FocusAdapter | focusGained focusLost |
ItemListener | none | itemStateChanged |
KeyListener | KeyAdapter | keyPressed keyReleased
keyTyped |
MouseListener | MouseAdapter | mouseClicked mouseEntered mouseExited
mousePressed mouseReleased |
MouseMotionListener | MouseMotionAdapter | mouseDragged
mouseMoved |
TextListener | none | textValueChanged |
WindowListener | WindowAdapter | windowActivated windowClosed windowClosing
windowDeactivated windowDeiconified windowIconified
windowOpened |
They represent window-system occurrences or low-level input:
mouse and key events -- both of which result directly from user input .
Most AWT listener interfaces contain more than one method.
For example, the MouseListener
interface:
mousePressed
mouseReleased
mouseEntered
mouseExited
mouseClicked
The AWT provides an adapter class for each listener interface
with more than one method. For example, the
MouseAdapter
class implements the
MouseListener
interface.
An adapter class implements empty versions of all its interface's methods:
/* * An example of extending an adapter class instead of * directly implementing a listener interface. */ public class MyClass extends MouseAdapter { ... someObject.addMouseListener(this); ... public void mouseClicked(MouseEvent e) { ...//Event handler implementation goes here... } }
Copyright © 1998-2009 Dilvan Moreira