The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search

Trail: Creating a GUI with JFC/Swing
Lesson: Writing Event Listeners

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(in the Creating a GUI with JFC/Swing trail), chooses a menu item(in the Creating a GUI with JFC/Swing trail) or presses Return in a text field(in the Creating a GUI with JFC/Swing trail), 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, Some Simple Event-Handling Examples. You can find the entire program in Beeper.java(in a .java source file). The other example described in that section, MultiListener.java(in a .java source file), has two action sources and two action listeners, with one listener listening to both sources and the other listening to just one.

The Action Event API

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 just after the user informs the listened-to component that an action should occur.
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 fire action events 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 ActionEvent inherits from EventObject(in the API reference documentation).

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 Some Simple Event-Handling Examples Contains one button with one action listener that beeps when you click the button.
MultiListener Some Simple Event-Handling Examples Registers two different action listeners on one button. Also registers the same action listener on two different buttons.
RadioButtonDemo.java How to Use Radio Buttons(in the Creating a GUI with JFC/Swing trail) 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(in the Creating a GUI with JFC/Swing trail) Shows how to listen for action events on menu items.
TextDemo How to Use Text Fields(in the Creating a GUI with JFC/Swing trail) An applet that registers an action listener on a text field.
ActionDemo How to Use Actions(in the Creating a GUI with JFC/Swing trail) Uses actions to bind buttons and menu items to the same function.
IconDemoApplet How to Use Icons(in the Creating a GUI with JFC/Swing trail) 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(in the Creating a GUI with JFC/Swing trail) Registers an action listener through a factory method on the OK button of a color chooser dialog.
SliderDemo How to Use Sliders(in the Creating a GUI with JFC/Swing trail) Registers an action listener on a timer that controls an animation loop.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search