[Chapter 20] The java.awt.event Package

#

| Chapter 20 |

# 20. The java.awt.event Package

Contents:
java.awt.event.ActionListener (JDK 1.1)
java.awt.event.AdjustmentEvent (JDK 1.1)
java.awt.event.AdjustmentListener (JDK 1.1)
java.awt.event.ComponentAdapter (JDK 1.1)
java.awt.event.ComponentEvent (JDK 1.1)
java.awt.event.ComponentListener (JDK 1.1)
java.awt.event.ContainerAdapter (JDK 1.1)
java.awt.event.ContainerEvent (JDK 1.1)
java.awt.event.ContainerListener (JDK 1.1)
java.awt.event.FocusAdapter (JDK 1.1)
java.awt.event.FocusEvent (JDK 1.1)
java.awt.event.FocusListener (JDK 1.1)
java.awt.event.InputEvent (JDK 1.1)
java.awt.event.ItemEvent (JDK 1.1)
java.awt.event.ItemListener (JDK 1.1)
java.awt.event.KeyAdapter (JDK 1.1)
java.awt.event.KeyEvent (JDK 1.1)
java.awt.event.KeyListener (JDK 1.1)
java.awt.event.MouseAdapter (JDK 1.1)
java.awt.event.MouseEvent (JDK 1.1)
java.awt.event.MouseListener (JDK 1.1)
java.awt.event.MouseMotionAdapter (JDK 1.1)
java.awt.event.MouseMotionListener (JDK 1.1)
java.awt.event.PaintEvent (JDK 1.1)
java.awt.event.TextEvent (JDK 1.1)
java.awt.event.TextListener (JDK 1.1)
java.awt.event.WindowAdapter (JDK 1.1)
java.awt.event.WindowEvent (JDK 1.1)
java.awt.event.WindowListener (JDK 1.1)

This package defines classes and interfaces used for event handling in the AWT. This package, and all of its classes and interfaces, are new in Java 1.1.

The members of this package fall into three categories:

- Events. The classes with names ending in "Event" represent specific types of events, generated by the AWT or by one of the AWT components.

- Listeners. The interfaces in this package are all "event listeners"; their names end with "Listener." These interfaces define the methods that must be implemented by any object that wants to be notified when a particular event occurs. Note that there is a `Listener` interface for each `Event` class.

- Adapters. Each of the classes with a name ending in "Adapter" provides a no-op implementation for an event listener interface that defines more than one method. When you are only interested in a single method of an event listener interface, it is easier to subclass an `Adapter` class than to implement all of the methods of the corresponding `Listener` interface.

Figure 20.1 shows the class hierarchy for this package. See Chapter 7, Events, for more information on events and event handling.

#### Figure 20.1: The java.awt.event package

## 20.1 java.awt.event.ActionEvent (JDK 1.1)

###

An object of this class represents a high-level "action" event generated by an AWT component. Instead of representing a direct user event, such as a mouse or keyboard event, `ActionEvent` represents some sort of action performed by the user on an AWT component.

The `getID()` method returns the type of action that has occurred. For AWT-generated action events, this type is always `ActionEvent.ACTION_PERFORMED`; custom components can generate action events of other types.

The `getActionCommand()` method returns a `String` that serves as a kind of name for the action that the event represents. The `Button` and `MenuItem` components have a `setActionCommand()` method that allows the programmer to specify an "action command" string to be included with any action events generated by those components. It is this value that is returned by the `getActionCommand()` method. When more than one `Button` or other component notifies the same `ActionListener`, you can use `getActionCommand()` to help determine the appropriate response to the event. This is generally a better technique than using the source object returned by `getSource()`. If no action command string is explicitly set, `getActionCommand` returns the label of the `Button` or `MenuItem`. Note, however, that internationalized programs should not rely on these labels to be constant.

Finally, `getModifiers()` returns a value that indicates what keyboard modifiers were in effect when the action event was triggered. Use the various `_MASK` constants, along with the `&` operator to decode this value.

    public class ActionEvent extends AWTEvent {
        // Public Constructors
                public ActionEvent(Object source, int id, String command);
                public ActionEvent(Object source, int id, String command, int modifiers);
        // Constants
                public static final int ACTION_FIRST;
                public static final int ACTION_LAST;
                public static final int ACTION_PERFORMED;
                public static final int ALT_MASK;
                public static final int CTRL_MASK;
                public static final int META_MASK;
                public static final int SHIFT_MASK;
        // Public Instance Methods
                public String getActionCommand();
                public int getModifiers();
                public String paramString();  // Overrides AWTEvent
    }

### Hierarchy:

`Object->EventObject(Serializable)->AWTEvent->ActionEvent`

### Passed To:

`ActionListener.actionPerformed(), AWTEventMulticaster.actionPerformed(), Button.processActionEvent(), List.processActionEvent(), MenuItem.processActionEvent(), TextField.processActionEvent()`

java.awt.datatransfer.UnsupportedFlavorException (JDK 1.1) |  | java.awt.event.ActionListener (JDK 1.1)