Previous | Next | Trail Map | Creating a User Interface (AWT Only) | Using Components, the GUI Building Blocks

Writing an Action Listener

Action listeners are probably the easiest -- and most common -- event handlers to implement. You implement an action listener to respond to the user's indication that some implementation-dependent action should occur.

When the user clicks a button, doubleclicks a list item, chooses a menu item, or presses return in a text field, an action event occurs. The result is that an actionPerformed message is sent to all action listeners that are registered on the relevant component.

Action Event Methods

The ActionListener(in the API reference documentation) interface contains a single method, and thus has no corresponding adapter class. Here is the lone ActionListener method:
void actionPerformed(ActionEvent)
Called by the AWT just after the user informs the listened-to component that an action should occur.

Examples of Handling Action Events

Here is the action event handling code from an applet named Beeper:
public class Beeper ...  implements ActionListener {
    ...
    //where initialization occurs:
        button.addActionListener(this);
    ...
    public void actionPerformed(ActionEvent e) {
	...//Make a beep sound...
    }
}
The Beeper applet is described in this trail's introduction to events, Introduction to the 1.1 AWT Event Model. You can find the entire program in Beeper.java.

You can find examples of action listeners in the following sections:

A Simple Example
Contains and explains the applet whose code is shown above.
A More Complex Example
Contains and explains an applet that has two action sources and two action listeners, with one listener listening to both sources and the other listening to just one.

Here are some more of the many source files in this tutorial that contain action listeners:

The ActionEvent Class

The actionPerformed method has a single parameter: an ActionEvent(in the API reference documentation) object. The ActionEvent class defines two useful methods:
String getActionCommand()
Returns the string associated with this action. Most objects that can generate actions support a method called setActionCommand that lets you set this string. If you don't set the action command explicitly, then it's generally the text displayed in the component. For objects with multiple items, and thus multiple possible actions, the action command is generally the name of the selected item.
int getModifiers()
Returns an integer representing the modifier keys the user was pressing when the action event occurred. You can use the ActionEvent-defined constants SHIFT_MASK, CTRL_MASK, META_MASK, and ALT_MASK to determine which keys were pressed. For example, if the user Shift-selects a menu item, then the following expression is nonzero:
actionEvent.getModifiers() & ActionEvent.SHIFT_MASK
    
Also useful is the getSource method, which returns the object (a component or menu component) that generated the action event. The getSource method is defined in one of ActionEvent's superclasses, EventObject(in the API reference documentation).


Previous | Next | Trail Map | Creating a User Interface (AWT Only) | Using Components, the GUI Building Blocks