The classes that represent events are at the core of Java’s event handling mechanism. Thus, a discussion of event handling must begin with the event classes. It is important to understand, however, that Java defines several types of events and that not all event classes can be discussed in this chapter. Arguably, the most widely used events at the time of this writing are those defined by the AWT and those defined by Swing. This chapter focuses on the AWT events. (Most of these events also apply to Swing.) Several Swing-specific events are described in Chapter 31, when Swing is covered.
At the root of the Java event class hierarchy is EventObject, which is in java.util. It is the superclass for all events. Its one constructor is shown here:
Here, src is the object that generates this event.
EventObject defines two methods: getSource( ) and toString( ). The getSource( ) method returns the source of the event. Its general form is shown here:
Object getSource( )
As expected, toString( ) returns the string equivalent of the event.
The class AWTEvent, defined within the java.awt package, is a subclass of EventObject. It is the superclass (either directly or indirectly) of all AWT-based events used by the delegation event model. Its getID( ) method can be used to determine the type of the event. The signature of this method is shown here:
int getID( )
Additional details about AWTEvent are provided at the end of Chapter 26. At this point, it is important to know only that all of the other classes discussed in this section are subclasses of AWTEvent.
EventObject is a superclass of all events.
AWTEvent is a superclass of all AWT events that are handled by the delegation event model.
The package java.awt.event defines many types of events that are generated by various user interface elements. Table 24-1 shows several commonly used event classes and provides a brief description of when they are generated. Commonly used constructors and methods in each class are described in the following sections.
Event Class : Description
ActionEvent : Generated when a button is pressed, a list item is double-clicked, or a menu item is selected.
AdjustmentEvent : Generated when a scroll bar is manipulated.
ComponentEvent : Generated when a component is hidden, moved, resized, or becomes visible.
ContainerEvent : Generated when a component is added to or removed from a container.
FocusEvent : Generated when a component gains or loses keyboard focus.
InputEvent : Abstract superclass for all component input event classes.
ItemEvent : Generated when a check box or list item is clicked; also occurs when a choice selection is made or a checkable menu item is selected or deselected.
KeyEvent : Generated when input is received from the keyboard.
MouseEvent : Generated when the mouse is dragged, moved, clicked, pressed, or released; also generated when the mouse enters or exits a component.
MouseWheelEvent : Generated when the mouse wheel is moved.
TextEvent : Generated when the value of a text area or text field is changed.
WindowEvent : Generated when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit.
Table 24-1 Commonly Used Event Classes in java.awt.event
The ActionEvent Class
An ActionEvent is generated when a button is pressed, a list item is double-clicked, or a menu item is selected. The ActionEvent class defines four integer constants that can be used to identify any modifiers associated with an action event: ALT_MASK, CTRL_MASK, META_MASK, and SHIFT_MASK. In addition, there is an integer constant, ACTION_PERFORMED, which can be used to identify action events.
ActionEvent has these three constructors:
ActionEvent(Object src, int type, String cmd) ActionEvent(Object src, int type, String cmd, int modifiers)
ActionEvent(Object src, int type, String cmd, long when, int modifiers)
Here, src is a reference to the object that generated this event. The type of the event is specified by type, and its command string is cmd. The argument modifiers indicates which modifier keys (alt, ctrl, meta, and/or shift) were pressed when the event was generated. The when parameter specifies when the event occurred.
You can obtain the command name for the invoking ActionEvent object by using the getActionCommand( ) method, shown here:
String getActionCommand( )
For example, when a button is pressed, an action event is generated that has a command name equal to the label on that button.
The getModifiers( ) method returns a value that indicates which modifier keys (alt, ctrl, meta, and/or shift) were pressed when the event was generated. Its form is shown here:
int getModifiers( )
The method getWhen( ) returns the time at which the event took place. This is called the event’s timestamp. The getWhen( ) method is shown here:
long getWhen( )
The AdjustmentEvent Class
An AdjustmentEvent is generated by a scroll bar. There are five types of adjustment events. The AdjustmentEvent class defines integer constants that can be used to identify them. The constants and their meanings are shown here:
BLOCK_DECREMENT : The user clicked inside the scroll bar to decrease its value.
BLOCK_INCREMENT : The user clicked inside the scroll bar to increase its value.
TRACK : The slider was dragged.
UNIT_DECREMENT : The button at the end of the scroll bar was clicked to decrease its value.
UNIT_INCREMENT : The button at the end of the scroll bar was clicked to increase its value.
In addition, there is an integer constant, ADJUSTMENT_VALUE_CHANGED, that indicates that a change has occurred.
Here is one AdjustmentEvent constructor: AdjustmentEvent(Adjustable src, int id, int type, int val)
Here, src is a reference to the object that generated this event. The id specifies the event. The type of the adjustment is specified by type, and its associated value is val.
The getAdjustable( ) method returns the object that generated the event. Its form is shown here:
Adjustable getAdjustable( )
The type of the adjustment event may be obtained by the getAdjustmentType( ) method. It returns one of the constants defined by AdjustmentEvent. The general form is shown here:
int getAdjustmentType( )
The amount of the adjustment can be obtained from the getValue( ) method, shown here: int getValue( )
For example, when a scroll bar is manipulated, this method returns the value represented by that change.
The ComponentEvent Class
A ComponentEvent is generated when the size, position, or visibility of a component is changed. There are four types of component events. The ComponentEvent class defines integer constants that can be used to identify them. The constants and their meanings are shown here:
COMPONENT_HIDDEN : The component was hidden.
COMPONENT_MOVED : The component was moved.
COMPONENT_RESIZED : The component was resized.
COMPONENT_SHOWN : The component became visible.
ComponentEvent has this constructor: ComponentEvent(Component src, int type)
Here, src is a reference to the object that generated this event. The type of the event is specified by type.
ComponentEvent is the superclass either directly or indirectly of ContainerEvent,
FocusEvent, KeyEvent, MouseEvent, and WindowEvent, among others.
The getComponent( ) method returns the component that generated the event. It is shown here:
Component getComponent( )
The ContainerEvent Class
A ContainerEvent is generated when a component is added to or removed from a container. There are two types of container events. The ContainerEvent class defines int constants that can be used to identify them: COMPONENT_ADDED and COMPONENT_REMOVED. They indicate that a component has been added to or removed from the container.
ContainerEvent is a subclass of ComponentEvent and has this constructor: ContainerEvent(Component src, int type, Component comp)
Here, src is a reference to the container that generated this event. The type of the event is specified by type, and the component that has been added to or removed from the container is comp.
You can obtain a reference to the container that generated this event by using the getContainer ( ) method, shown here:
Container getContainer( )
The getChild( ) method returns a reference to the component that was added to or removed from the container. Its general form is shown here:
Component getChild( )
The FocusEvent Class
A FocusEvent is generated when a component gains or loses input focus. These events are identified by the integer constants FOCUS_GAINED and FOCUS_LOST.
FocusEvent is a subclass of ComponentEvent and has these constructors:
FocusEvent(Component src, int type) FocusEvent(Component src, int type, boolean temporaryFlag)
FocusEvent(Component src, int type, boolean temporaryFlag, Component other)
Here, src is a reference to the component that generated this event. The type of the event is specified by type. The argument temporaryFlag is set to true if the focus event is temporary. Otherwise, it is set to false. (A temporary focus event occurs as a result of another user interface operation. For example, assume that the focus is in a text field. If the user moves the mouse to adjust a scroll bar, the focus is temporarily lost.)
The other component involved in the focus change, called the opposite component, is passed in other. Therefore, if a FOCUS_GAINED event occurred, other will refer to the component that lost focus. Conversely, if a FOCUS_LOST event occurred, other will refer to the component that gains focus.
You can determine the other component by calling getOppositeComponent( ), shown here: Component getOppositeComponent( )
The opposite component is returned.
The isTemporary( ) method indicates if this focus change is temporary. Its form is shown here:
boolean isTemporary( )
The method returns true if the change is temporary. Otherwise, it returns false.
The InputEvent Class
The abstract class InputEvent is a subclass of ComponentEvent and is the superclass for component input events. Its subclasses are KeyEvent and MouseEvent.
InputEvent defines several integer constants that represent any modifiers, such as the control key being pressed, that might be associated with the event. Originally, the InputEvent class defined the following eight values to represent the modifiers:
However, because of possible conflicts between the modifiers used by keyboard events and mouse events, and other issues, the following extended modifier values were added:
When writing new code, it is recommended that you use the new, extended modifiers rather than the original modifiers.
To test if a modifier was pressed at the time an event is generated, use the isAltDown( ), isAltGraphDown( ), isControlDown( ), isMetaDown( ), and isShiftDown( ) methods. The forms of these methods are shown here:
boolean isAltDown( ) boolean isAltGraphDown( ) boolean isControlDown( ) boolean isMetaDown( ) boolean isShiftDown( )
You can obtain a value that contains all of the original modifier flags by calling the getModifiers( ) method. It is shown here:
int getModifiers( )
You can obtain the extended modifiers by calling getModifiersEx( ), which is shown here: int getModifiersEx( )
The ItemEvent Class
An ItemEvent is generated when a check box or a list item is clicked or when a checkable menu item is selected or deselected. (Check boxes and list boxes are described later in this book.) There are two types of item events, which are identified by the following integer constants:
DESELECTED : The user deselected an item.
SELECTED : The user selected an item.
In addition, ItemEvent defines one integer constant, ITEM_STATE_CHANGED, that signifies a change of state.
ItemEvent has this constructor:
ItemEvent(ItemSelectable src, int type, Object entry, int state)
Here, src is a reference to the component that generated this event. For example, this might be a list or choice element. The type of the event is specified by type. The specific item that generated the item event is passed in entry. The current state of that item is in state.
The getItem( ) method can be used to obtain a reference to the item that changed. Its signature is shown here:
Object getItem( )
The getItemSelectable( ) method can be used to obtain a reference to the ItemSelectable object that generated an event. Its general form is shown here:
ItemSelectable getItemSelectable( )
Lists and choices are examples of user interface elements that implement the
The getStateChange( ) method returns the state change (that is, SELECTED or DESELECTED) for the event. It is shown here:
int getStateChange( )