Download the JDK
Search the Tutorials
Hide the TOC
Trail: Creating a GUI with JFC/Swing
Lesson: Writing Event Listeners
Section: Implementing Listeners for Commonly Handled Events
Writing Event Listeners
Introduction to Event Listeners
General Information about Writing Event Listeners
Listeners Supported by Swing Components
Implementing Listeners for Commonly Handled Events
How to Write an Action Listener
How to Write a Caret Listener
How to Write a Change Listener
How to Write a Component Listener
How to Write a Container Listener
How to Write a Document Listener
How to Write a Focus Listener
How to Write an Internal Frame Listener
How to Write an Item Listener
How to Write a Key Listener
How to Write a List Data Listener
How to Write a List Selection Listener
How to Write a Mouse Listener
How to Write a Mouse-Motion Listener
How to Write a Mouse-Wheel Listener
How to Write a Property Change Listener
How to Write a Table Model Listener
How to Write a Tree Expansion Listener
How to Write a Tree Model Listener
How to Write a Tree Selection Listener
How to Write a Tree-Will-Expand Listener
How to Write an Undoable Edit Listener
How to Write Window Listeners
Listener API Table
Solving Common Event-Handling Problems
Home Page > Creating a GUI with JFC/Swing > Writing Event Listeners
« Previous • Trail • Next »
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 • Trail • Next »
Previous page: Implementing Listeners for Commonly Handled Events
Next page: How to Write a Caret Listener