home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Java in a Nutshell

Previous Chapter 20 Next

20. The java.awt.event Package

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.

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



Passed To:

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

Previous Home Next
java.awt.datatransfer.UnsupportedFlavorException (JDK 1.1) Book Index java.awt.event.ActionListener (JDK 1.1)

Java in a Nutshell Java Language Reference Java AWT Java Fundamental Classes Exploring Java