your tool for Java Certification
home | tutorial | questions | test 1

AWT Event Classes

This topic is part of SCJP 1.2 exam but not SCJP 1.4 exam.

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 classes
The 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.

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

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 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 -

// A window with just an OK button.
public class exampleWindow extends Frame {
        Button OkButton;
        OkHandler handler;

        public exampleWindow() { //Constructor
                .... /* Code to create a window, button 
and set a layout Manager. */
                /* Now create and add listener */
                handler = new OkHandler(this);

        public static void main (String args []) {
                new exampleWindow();

/* Now define a listener object. It must implement the 
interface ActionListener by defining the function 
actionPerformed. */
class OkHandler implements ActionListener {
        private exampleWindow win;
        public OkHandler(exampleWindow window) {
        public void actionPerformed(ActionEvent evt) {
                // Process clicking of button here.

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 TypeEvent SourceEvent Listener interface
ActionEventButton, List, MenuItem, TextFieldActionListener
ItemEventChoice, Checkbox, CheckboxMenuItem, ListItemListener
TextEventTextArea, TextFieldTextListener
MouseEventComponentMouseListener, MouseMotionListener

Table Event Listener Interfaces and corresponding methods which it defines
Event Listener interface Event Listener Methods
ActionListeneractionPerformed(ActionEvent evt)
AdjustmentListeneradjustmentValueChanged(AjustmentEvent evt)
ItemListeneritemStateChanged(ItemEvent evt)
TextListenertextValueChanged(TextEvent evt)
ComponentListenercomponentHidden(ComponentEvent evt), componentMoved(ComponentEvent evt), componentResized(ComponentEvent evt), componentShown(ComponentEvent evt)
ContainerListenercomponentAdded(ContainerEvent evt), componentRemoved(ContainerEvent evt)
FocusListenerfocusGained(FocusEvent evt), focusLost(FocusEvent evt)
KeyListenerkeyPressed(KeyEvent evt), keyReleased(KeyEvent evt), keyTyped(KeyEvent evt)
MouseListenermouseClicked(MouseEvent evt), mouseEntered(MouseEvent evt), mouseExited(MouseEvent evt), mousePressed(MouseEvent evt), mouseReleased(MouseEvent evt)
MouseMotionListenermouseDragged(MouseEvent evt), mouseMoved(MouseEvent evt)
WindowListenerwindowActivated(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
home | tutorial | questions | test 1

Last Updated on ... August 21st, 2000