Trail: Creating a GUI with JFC/Swing
Lesson: Writing Event Listeners
Section: Implementing Listeners for Commonly Handled Events
How to Write an Action Listener
How to Write 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, chooses a menu item or presses Enter 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.

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) {
	Toolkit.getDefaultToolkit().beep();
    }
}
The Beeper applet is described in this trail's introduction to events, Introduction to Event Listeners. You can find the entire program in Beeper.java. The other example described in that section, MultiListener.java, has two action sources and two action listeners, with one listener listening to both sources and the other listening to just one.

The Action Listener API

The ActionListener Interface

Because ActionListener has only one method, it has no corresponding adapter class.
Method Purpose
actionPerformed(actionEvent) Called just after the user informs the listened-to component that an action should occur.

The ActionEvent Class

Method Purpose
String getActionCommand() Returns the string associated with this action. Most objects that can fire action events support a method called setActionCommand that lets you set this string.
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
    
Object getSource()
(in java.util.EventObject)
Returns the object that fired the event.

Examples that Use Action Listeners

The following table lists some of the many examples that use action listeners.

Example Where Described Notes
Beeper This section and Introduction to Event Listeners Contains one button with one action listener that beeps when you click the button.
MultiListener Introduction to Event Listeners Registers two different action listeners on one button. Also registers the same action listener on two different buttons.
RadioButtonDemo How to Use Radio Buttons Registers the same action listener on five radio buttons. The listener uses the getActionCommand method to determine which radio button fired the event.
MenuDemo How to Use Menus Shows how to listen for action events on menu items.
DragPictureDemo2 Adding Cut/Copy/Paste Support to Data Transfer Uses setActionCommand to attach the cut, copy, and paste actions to the menu. Then uses as action listener to forward the cut/copy/paste actions to the currently focused component.
TextDemo How to Use Text Fields An applet that registers an action listener on a text field.
IconDemoApplet How to Use Icons Loads an image in an action listener. Because loading an image can take a while, this program uses a SwingWorker to load the image in a background thread.
TableDialogEditDemo How to Use Tables Registers an action listener through a factory method on the OK button of a color chooser dialog.
SliderDemo How to Use Sliders Registers an action listener on a timer that controls an animation loop.

Previous page: Implementing Listeners for Commonly Handled Events
Next page: How to Write a Caret Listener