In this section, you will learn how to handle events in
Java awt. Events are the integral part of the java platform. You can see the concepts related to the event handling through
the example and use methods through which you can implement the event driven application.
For any event to occur, the objects
registers themselves as listeners. No event takes place if there is no
listener i.e. nothing happens when an event takes place if there is no listener.
No matter how many listeners there are, each and every listener is capable of
processing an event. For example, a SimpleButtonEvent applet registers itself as the listener for the button's action
events that creates a Button instance.
ActionListener can be implemented by any Class
including Applet. One point to remember here is that all the listeners are always notified.
Moreover, you can also call AWTEvent.consume() method whenever you don't
want an event to be processed further. There is another method which is used by
a listener to check for the consumption. The method is isConsumed() method. The
processing of the events gets stopped with the consumption of the events by the
system once a listener is notified. Consumption only works for InputEvent and its
subclasses. Moreover, if you don't want any input from the user through keyboard
then you can use consume() method for the KeyEvent.
The step by step procedure of Event handling is as
- When anything interesting happens then the subclasses of
AWTEvent are generated by the component.
- Any class can act like a Listener class permitted by
the Event sources. For example, addActionListener() method is used
for any action to be performed, where Action is the event type. There
is another method by which you can remove the listener class which is
removeXXXListener() method, where XXX is the event type.
- A listener type has to be implemented for an event
handling such as ActionListener.
- There are some special type of listener types as
well for which you need to implement multiple methods like key Events.
There are three methods which are required to be implemented for Key events
and to register them i.e. one for key release, key typed and one for key
press. There are some special classes as well which are known as adapters
that are used to implement the listener interfaces and stub out all the methods.
these adapter classes can be sub classed and and can override the necessary
Most of the times every event-type has Listener
interface as Events subclass the AWTEvent class. However, PaintEvent
and InputEvent don't have the Listener interface because only the paint()
method can be overriden with PaintEvent etc.
A low-level input or window operation is represented
Low-level events. Types of
Low-level events are mouse movement, window opening, a key press etc. For
example, three events are generated by typing the letter 'A' on the Keyboard one for releasing, one for pressing, and one for
different type of low-level events and operations that generate each event
are show below in the form of a table.
|| Used for Getting/losing focus.
|| Used for entering, exiting,
clicking, dragging, moving, pressing, or releasing.
|| Used for Adding/removing component.
|| Used for releasing, pressing, or typing (both) a key.
|| Used for opening, deactivating, closing,
Iconifying, deiconifying, really closed.
|| Used for moving, resizing,
The interaction with GUI component is represented by
Semantic events like changing the text of a text field, selecting a button
etc. The different events generated by different components is shown
|| Used for state changed.
|| Used for do the command.
|| Used for text changed.
|| Used for value adjusted.
If a component is an event source for something then
the same happens with its subclasses. The different event sources are
represented by the following table.
| Low-Level Events
| Semantic Events
Every listener interface has at least one event type.
Moreover, it also contains a method for each type of event the event class
incorporates. For example as discussed earlier, the KeyListener
has three methods, one for each type of event that the KeyEvent has:
keyTyped(), keyPressed(), and keyReleased().
The Listener interfaces and their methods
are as follow:
|| windowActivated(WindowEvent e)
| windowDeiconified(WindowEvent e)
| windowOpened(WindowEvent e)
| windowClosed(WindowEvent e)
| windowClosing(WindowEvent e)
| windowIconified(WindowEvent e)
| windowDeactivated(WindowEvent e)
|| actionPerformed(ActionEvent e)
|| mouseClicked(MouseEvent e)
| mouseEntered(MouseEvent e)
| mouseExited(MouseEvent e)
| mousePressed(MouseEvent e)
| mouseReleased(MouseEvent e)
|| focusGained(FocusEvent e)
| focusLost(FocusEvent e)
|| itemStateChanged(ItemEvent e)
|| keyReleased(KeyEvent e)
|| keyTyped(KeyEvent e)
|| keyPressed(KeyEvent e)
|| componentHidden(ComponentEvent e)
| componentMoved(ComponentEvent e)
| componentShown(ComponentEvent e)
| componentResized(ComponentEvent e)
|| mouseMoved(MouseEvent e)
|| mouseDragged(MouseEvent e)
|| textValueChanged(TextEvent e)
| ContainerListen er
|| componentAdded(ContainerEvent e)
|| componentRemoved(ContainerEvent e)