| Event, listener interface and add-and remove-methods | Components supporting this event |
| ActionEvent
ActionListener addActionListener( ) removeActionListener( ) |
Button, List, TextField, MenuItem, and its derivatives including CheckboxMenuItem, Menu, and PopupMenu |
| AdjustmentEvent
AdjustmentListener addAdjustmentListener( ) removeAdjustmentListener( ) |
Scrollbar Anything you create that implements the Adjustable interface |
| ComponentEvent
ComponentListener addComponentListener( ) removeComponentListener( ) |
Component and its derivatives, including Button, Canvas, Checkbox, Choice, Container, Panel, Applet, ScrollPane, Window, Dialog, FileDialog, Frame, Label, List, Scrollbar, TextArea, and TextField |
| ContainerEvent
ContainerListener addContainerListener( ) removeContainerListener( ) |
Container and its derivatives, including Panel, Applet, ScrollPane, Window, Dialog, FileDialog, and Frame |
| FocusEvent
FocusListener addFocusListener( ) removeFocusListener( ) |
Component and its derivatives, including Button, Canvas, Checkbox, Choice, Container, Panel, Applet, ScrollPane, Window, Dialog, FileDialog, Frame Label, List, Scrollbar, TextArea, and TextField |
| KeyEvent
KeyListener addKeyListener( ) removeKeyListener( ) |
Component and its derivatives, including Button, Canvas, Checkbox, Choice, Container, Panel, Applet, ScrollPane, Window, Dialog, FileDialog, Frame, Label, List, Scrollbar, TextArea, and TextField |
| MouseEvent (for both clicks and motion)
MouseListener addMouseListener( ) removeMouseListener( ) |
Component and its derivatives, including Button, Canvas, Checkbox, Choice, Container, Panel, Applet, ScrollPane, Window, Dialog, FileDialog, Frame, Label, List, Scrollbar, TextArea, and TextField |
| MouseEvent (for both clicks and motion)
MouseMotionListener addMouseMotionListener( ) removeMouseMotionListener( ) |
Component and its derivatives, including Button, Canvas,
Checkbox, Choice, Container,
Panel, Applet, ScrollPane, Window, Dialog, FileDialog, Frame, Label, List, Scrollbar, TextArea, and TextField |
| WindowEvent
WindowListener addWindowListener( ) removeWindowListener( ) |
Window and its derivatives, including Dialog, FileDialog, and Frame |
| ItemEvent
ItemListener addItemListener( ) removeItemListener( ) |
Checkbox, CheckboxMenuItem, Choice, List, and anything that implements the ItemSelectable interface |
| TextEvent
TextListener addTextListener( ) removeTextListener( ) |
Anything derived from TextComponent, including TextArea and TextField |
You can see that each type of component supports only certain types
of events. It's helpful to see the events supported by each component,
as shown in the following table:
| Component type | Events supported by this component |
| Adjustable | AdjustmentEvent |
| Applet | ContainerEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| Button | ActionEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| Canvas | FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| Checkbox | ItemEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| CheckboxMenuItem | ActionEvent, ItemEvent |
| Choice | ItemEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| Component | FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| Container | ContainerEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| Dialog | ContainerEvent, WindowEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| FileDialog | ContainerEvent, WindowEvent,FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| Frame | ContainerEvent, WindowEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| Label | FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| List | ActionEvent, FocusEvent, KeyEvent, MouseEvent, ItemEvent, ComponentEvent |
| Menu | ActionEvent |
| MenuItem | ActionEvent |
| Panel | ContainerEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| PopupMenu | ActionEvent |
| Scrollbar | AdjustmentEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| ScrollPane | ContainerEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| TextArea | TextEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| TextComponent | TextEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| TextField | ActionEvent, TextEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| Window | ContainerEvent, WindowEvent,
FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
| Listener interface
w/ adapter |
Methods in interface |
| ActionListener | actionPerformed(ActionEvent) |
| AdjustmentListener | adjustmentValueChanged(AdjustmentEvent) |
| ComponentListener
ComponentAdapter |
componentHidden(ComponentEvent)
componentShown(ComponentEvent) componentMoved(ComponentEvent) componentResized(ComponentEvent) |
| ContainerListener
ContainerAdapter |
componentAdded(ContainerEvent)
componentRemoved(ContainerEvent) |
| FocusListener
FocusAdapter |
focusGained(FocusEvent)
focusLost(FocusEvent) |
| KeyListener
KeyAdapter |
keyPressed(KeyEvent)
keyReleased(KeyEvent) keyTyped(KeyEvent) |
| MouseListener
MouseAdapter |
mouseClicked(MouseEvent)
mouseEntered(MouseEvent) mouseExited(MouseEvent) mousePressed(MouseEvent) mouseReleased(MouseEvent) |
| MouseMotionListener
MouseMotionAdapter |
mouseDragged(MouseEvent)
mouseMoved(MouseEvent) |
| WindowListener
WindowAdapter |
windowOpened(WindowEvent)
windowClosing(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) |
| ItemListener | itemStateChanged(ItemEvent) |
| TextListener | textValueChanged(TextEvent) |
In the table above, you can see that some listener interfaces have only one method. These are trivial to implement since you'll implement them only when you want to write that particular method. However, the listener interfaces that have multiple methods could be less pleasant to use. For example, something you must always do when creating an application is provide a WindowListener to the Frame so that when you get the windowClosing( ) event you can call System.exit(0) to exit the application. But since WindowListener is an interface, you must implement all of the other methods even if they don't do anything. This can be annoying.
To solve the problem, each of the listener interfaces that have more than one method are provided with adapters, the names of which you can see in the table above. Each adapter provides default methods for each of the interface methods. (Alas, WindowAdapter does not have a default windowClosing( ) that calls System.exit(0).) Then all you need to do is inherit from the adapter and override only the methods you need to change. For example, the typical WindowListener you'll use looks like this:
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}The whole point of the adapters is to make the creation of listener classes easy.
There is a downside to adapters, however, in the form of a pitfall. Suppose you write a WindowAdapter like the one above:
class MyWindowListener extends WindowAdapter {
public void WindowClosing(WindowEvent e) {
System.exit(0);
}
}This doesn't work, but it will drive you crazy trying to figure out why, since everything will compile and run fine -- except that closing the window won't exit the program. Can you see the problem? It's in the name of the method: WindowClosing( ) instead of windowClosing( ). A simple slip in capitalization results in the addition of a completely new method. However, this is not the method that's called when the window is closing, so you don't get the desired results.