AWT Event Classes


Events

When the user interacts with a GUI application, an event is generated. Examples of user events are clicking a button, selecting an item or closing a window. Events are represented as Objects in JavaTM technology. The super class of all event classes is java.util.EventObject. Some of the important classes and their hierarchy is shown below.

Some methods related to Events are

Object getSource()

This method is defined in the Event Object class. It returns the object that originated the event. So if the user has clicked a Push button, the method getSource returns the object corresponding to Push button is generated.

int getID()

This method returns the integer value corresponding to the type of event. For example if the user has clicked on the mouse, the method getID returns the integer defined as MouseEvent.MOUSE_CLICKED.AWT event classesThe subclasses of ATW Event can be categorized into two groups – Semantic events and low-level events. Semantic events directly correspond to high level user interactions with a GUI component. Clicking of a button is an example of a semantic event. Following event classes are semantic classes.

ActionEvent
Adjustment
Event Item
Event TextEvent

Multiple low-level events may get generated for each high level user event. Following event classes are low level event classes.

ComponentEvent
ContainerEvent
FocusEvent
KeyEvent
MouseEvent
PaintEvent
WindowEvent

Event classes for GUI controls:

By GUI component I mean objects like Button, ListBox etc. For each Java GUI component a set of events of above type are generated. It is important to understand which events are generated for each component. This is explained below.
The GUI component that generate ActionEvent are
Button : When the user clicks on a PushButton.
List : When an item in list box is double clicked.
MenuItem : When a MenuItem is selected.
TextField : When the user clicks the Enter key in a text box.

AdjustmentEvent:

Is generated when the user adjusts the position of a scrollbar. Scrollbar is the only GUI control that receives the AdjustmentEvent.

ItemEvent:

Is generated when an item is selected or deselected. Following components generate ItemEvents.
CheckBox : When the state of the CheckBox is changed.
CheckBoxMenuItem : When the state of a MenuItem is changed.
Choice : When the state of a ChoiceBox is changed.
List : When an item in list is selected or deselected.

TextEvent:

Is generated when the contents of text component are changed. The components that generate TextEvent are TextArea and TextField.Now I will describe the GUI components corresponding to low level event classes.

FocusEvent:

This event is generated when a component gains or looses focus. Focus may be gained by bringing the mouse over a component (or by using the tab key). The component that has the focus receives all user keyboard events. Focus events are generated by objects of Component class and all its subclasses.

KeyEvent and MouseEvent: 

These are subclasses of abstract InputEvent class. Both these events are generated by objects of type Component class and its subclasses. The KeyEvent is generated when the user presses or releases a key on the keyboard. The MouseEvent is generated when the user presses the mouse or moves the mouse.

WindowEvent:

Are generated for the Window class and its subclasses. These events are generated if an operation is performed on a window. The operation could be closing of window, opening of window, activating a window etc.

PaintEvent:

is generated when a component needs to be repainted (for example when an application which is in front is closed and the component needs to be redrawn.) PaintEvents are internally handled by AWT, and cannot (and should not) be handled by you in the application.

ComponentEvent:

Are also generated by objects of type Component class and its subclasses. This event is generated when a component is hidden, shown, moved or resized.

ContainerEvents:

Are generated when a component is added or removed from a container. ComponentEvent and ContainerEvent are handled by AWT and are not normally handled by the user.

Event Listeners:

These are objects that define methods to handle certain type of events. An event source (for example a PushButton) can generate one or more type of events, and maintain a list of event listeners for each type of event. An event source can register listeners by calling addXListener type of methods. For example a Button may register an object for handling ActionEvent by calling addActionListener. This object would then need to implement the listener interface corresponding to ActionEvent, which is ActionListener. The example below illustrates this –

So to set up the processing of events the following tasks must be done.

  1. For the GUI component (like pushbutton) associate a listener object class with the component by calling a method of type addXListener (See table below for list of methods).
  2. Define this listener object. It must implement the corresponding interface. The name of interface is of type EventListener. Table below gives list of event listeners.
  3. The object must define all the methods defined in the interface it is implementing. See table for the list of Event Listener methods defined in each Event Listener interface.

Table Event types and corresponding EventSource & EventListener : 

Event Type Event Source Event Listener interface
ActionEvent Button, List, MenuItem, TextField ActionListener
AdjustmentEvent Scrollbar AdjustmentListener
ItemEvent Choice, Checkbox, CheckboxMenuItem, List ItemListener
TextEvent TextArea, TextField TextListener
ComponentEvent Component ComponentListener
ContainerEvent Container ContainerListener
FocusEvent Component FocusListener
KeyEvent Component KeyListener
MouseEvent Component MouseListener, MouseMotionListener
WindowEvent Window WindowListener

Table Event Listener Interfaces and corresponding methods which it defines

Event Listener interface Event Listener Methods
ActionListener actionPerformed(ActionEvent evt)
AdjustmentListener adjustmentValueChanged(AjustmentEvent evt)
ItemListener itemStateChanged(ItemEvent evt)
TextListener textValueChanged(TextEvent evt)
ComponentListener componentHidden(ComponentEvent evt), componentMoved(ComponentEvent evt), componentResized(ComponentEvent evt), componentShown(ComponentEvent evt)
ContainerListener componentAdded(ContainerEvent evt), componentRemoved(ContainerEvent evt)
FocusListener focusGained(FocusEvent evt), focusLost(FocusEvent evt)
KeyListener keyPressed(KeyEvent evt), keyReleased(KeyEvent evt), keyTyped(KeyEvent evt)
MouseListener mouseClicked(MouseEvent evt), mouseEntered(MouseEvent evt), mouseExited(MouseEvent evt), mousePressed(MouseEvent evt), mouseReleased(MouseEvent evt)
MouseMotionListener mouseDragged(MouseEvent evt), mouseMoved(MouseEvent evt)
WindowListener windowActivated(WindowEvent evt), windowClosed(WindowEvent evt), windowClosing(WindowEvent evt), windowDeactivated(WindowEvent evt), windowDeiconified(WindowEvent evt), windowIconified(WindowEvent evt), windowOpened(WindowEvent evt)

Adapter classes:

Event adapters facilitate implementing listener interfaces. Many event listener interfaces have more than one event listener methods. For such interfaces, Java technology defines adapter classes. These have empty implementation (stubs) of all the event listener methods defined in the interface they implement. A listener can subclass the adapter and override only stub methods for handling events of interest. The table below lists the low level event listener interfaces and their adapters.

Table Event Listener Interfaces and their corresponding adapter classes.

Event Listener interface Event Listener Adapter
ComponentListener ComponentAdapter
ContainerListener ContainerAdapter
FocusListener FocusAdapter
KeyListener KeyAdapter
MouseListener MouseAdapter
MouseMotionListener MouseMotionAdapter
WindowListener WindowAdapter