How to Write an Action Listener

 <  Day Day Up  >  

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's 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();     } } 
graphics/cd_icon.gif

The Beeper example is described in Chapter 5's introduction to events, Some Event-Handling Examples (page 108). You can find the entire program in Beeper.java . [1] The other example described in that chapter, MultiListener.java , [2] has two action sources and two action listeners, with one listener listening to both sources and the other listening to just one.

[1] This source code is on the CD at: JavaTutorial/uiswing/events/example-1dot4/index. html#Beeper and online at: http://java.sun.com/docs/books/tutorial/uiswing/events/example-1dot4/index.html#Beeper.

[2] This source code is on the CD at: JavaTutorial/uiswing/events/example-1dot4/index. html#MultiListener and online at: http://java.sun.com/docs/books/tutorial/uiswing/events/example-1dot4/index.html#MultiListener.

The Action Listener API

The ActionListener interface [3] only has one method (see Table 1), so it does not have a corresponding adapter class. Table 2 lists the methods in the ActionEvent class. The ActionListener API documentation is online at: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ActionListener.html. The ActionEvent API documentation is online at: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ActionEvent.html.

[3] ActionListener API documentation: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ActionListener.html.

Table 1. The ActionListener Interface

Method

Purpose

actionPerformed(actionEvent)

Called just after the user informs the listened-to component that an action should occur.

Table 2. The ActionEvent Class

Method

Purpose

String getActionCommand()

Return 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()

Return 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) 

Return 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 Some Event-Handling Examples (page 108)

Contains one button with one action listener that beeps when you click the button.

MultiListener

Some Event-Handling Examples (page 108)

Registers two different action listeners on one button. Also registers the same action listener on two different buttons .

RadioButtonDemo

How to Use Radio Buttons (page 311)

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 (page 277)

Shows how to listen for action events on menu items.

DragPictureDemo2

How to Use Drag and Drop and Data Transfer (page 545)

Uses setActionCommand to attach the cut, copy, and paste actions to the menu. Then uses an action listener to forward the cut/copy/paste actions to the currently focused component.

TextDemo

How to Use Text Fields (page 423)

An applet that registers an action listener on a text field.

IconDemoApplet

How to Use Icons (page 603)

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 (page 388)

Registers an action listener through a factory method on the OK button of a color chooser dialog.

SliderDemo

How to Use Sliders (page 348)

Registers an action listener on a timer that controls an animation loop.

 <  Day Day Up  >  


JFC Swing Tutorial, The. A Guide to Constructing GUIs
The JFC Swing Tutorial: A Guide to Constructing GUIs (2nd Edition)
ISBN: 0201914670
EAN: 2147483647
Year: 2004
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net