Lesson Seventeen

Events and Event Listeners


In Lesson Eight, you learned how to use events to make your applets interactive. Now we're going to use inheritance and abstract classes to further the event handling process. This is where the EventObject, which is the parent to the AWTEvent comes into play. Below are some user actions and the event types associated with them:

User ActionEvent Type
Click a ButtonActionEvent
Click a ComponentMouseEvent
Click an item in a ChoiceItemEvent
Click an item in a CheckboxItemEvent
Change text in a TextFieldTextEvent
Open a windowWindowEvent
Iconify a windowWindowEvent
Type a keyKeyEvent

As you may have guessed, every xxxEvent requires an xxxListener, which has the return type void. Remembering that interfaces contain abstract methods, which are empty methods, in order to implement a listener, you must provide your own methods for all the methods that are part of the interface. Although you can leave the methods empty in your implementation. The following table shows the events, their listeners, and their handlers:

EventListenerHandlers
ActionEventActionListeneractionPerformed(ActionEvent)
ItemEventItemListeneritemStateChanged(ItemEvent)
TextEventTextListenertextValueChanged(TextEvent)
AdjustmentEventAdjustmentListeneradjustmentValueChanged(AdjustmentEvent)
ContainerEventContainerListenercomponentAdded(ContainerEvent)
componentRemoved(ContainerEvent)
ComponentEventComponentListenercomponentMoved(ComponentEvent)
componentHidden(ComponentEvent)
componentResized(ComponentEvent)
componentShown(ComponentEvent)
FocusEventFocusListenerfocusGained(FocusEvent)
focusLost(FocusEvent)
MouseEventMouseListenermousePressed(MouseEvent)
mouseReleased(MouseEvent)
mouseEntered(MouseEvent)
mouseExited(MouseEvent)
mouseClicked(MouseEvent)
MouseMotionListenermouseDragged(MouseEvent)
mouseMoved(MouseEvent)
KeyEventKeyListenerkeyPressed(KeyEvent)
keyTyped(KeyEvent)
keyReleased(KeyEvent)
WindowEventComponentListenerwindowMoved(WindowEvent)
windowActivated(WindowEvent)
windowClosing(WindowEvent)
windowClosed(WindowEvent)
windowDeiconified(WindowEvent)
windowIconified(WindowEvent)
windowOpened(WindowEvent)

Question: Now that I know all the listeners, is this where I get to learn the hanlers and how to use them?

Answer: Yes. We're going to handle exceptions using try and catch.


Lesson Sixteen | Home | Lesson Eighteen

If you have a question about any of the lessons, feel free to ask.

1