java.awt.event

There are two ways to handle events in Java 1.1. The first is to delegate event handling to a listener object. The second is to explicitly enable the originating component to handle its own events. Bruce Eckel describes this latter strategy as ugly, old stlye, hard to maintain and reuse. It will not be covered here.

An "event listener" is an object to which a component has delegated the task of handling a particular type of event. When the component experiences input, an event of the appropriate type is constructed and then sent as the parameter of a standard method called on the listener.

In the preceeding section, an ActionListener for the Button component was demonstrated. The required steps included:

  1. Define an inner class that implements ActionListener and provides a definition for all of the interface's abstract methods
  2. Create an instance of the Button class
  3. Create an instance of the "listener" class
  4. Register the listener instance with the widget instance by calling addActionListener() on the widget and passing the listener
There are 11 listener types (each represented by an interface). The table below summarizes all the abstract methods that can (and must) be implemented by each class assigned the role of "listener". In the previous section, we wanted to catch "windowClosing" events. If we had used the strategy listed above, an unneeded (or empty) implementation would have been required for the WindowListener's other six abstract methods - an exercise in tedium.

Fortunately, the java.awt.event package provides seven "adapter" classes, one for each listener interface that defines more than one abstract method. An "adapter" is simply a class that implements an interface by providing do-nothing methods. In our example, we defined a class that extends WindowAdapter, and then we only had to override the one method we were interested in - windowClosing().

[Source: Roberts, p313-318]

Interface/Adapter

methods

add method

ActionListener actionPerformed(ActionEvent) addActionListener()
AdjustmentListener adjustmentValueChanged
                          (AdjustmentEvent)
addAdjustmentListener()
ComponentListener
ComponentAdapter
componentHidden(ComponentEvent)
componentMoved(ComponentEvent)
componentResized(ComponentEvent)
componentShown(ComponentEvent)
addComponentListener()
ContainerListener
ContainerAdapter
componentAdded(ContainerEvent)
componentRemoved(ContainerEvent)
addContainerListener()
FocusListener
FocusAdapter
focusGained(FocusEvent)
focusLost(FocusEvent)
addFocusListener()
ItemListener itemStateChanged(ItemEvent) addItemListener()
KeyListener
KeyAdapter
keyPressed(KeyEvent)
keyReleased(KeyEvent)
keyTyped(KeyEvent)
addKeyListener()
MouseListener
MouseAdapter
mouseClicked(MouseEvent)
mouseEntered(MouseEvent)
mouseExited(MouseEvent)
mousePressed(MouseEvent)
mouseReleased(MouseEvent)
addMouseListener()
MouseMotionListener
MouseMotionAdapter
mouseDragged(MouseEvent)
mouseMoved(MouseEvent)
addMouseMotionListener()
TextListener textValueChanged(TextEvent) addTextListener()
WindowListener
WindowAdapter
windowActivated(WindowEvent)
windowClosed(WindowEvent)
windowClosing(WindowEvent)
windowDeactivated(WindowEvent)
windowDeiconified(WindowEvent)
windowIconified(WindowEvent)
windowOpened(WindowEvent)