|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
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
actionPerformedmessage 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:Thepublic class Beeper ... implements ActionListener { ... //where initialization occurs: button.addActionListener(this); ... public void actionPerformed(ActionEvent e) { Toolkit.getDefaultToolkit().beep(); } }Beeperapplet is described in this trail's introduction to events, Introduction to Event Listeners. You can find the entire program inBeeper.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.
Because
ActionListenerhas 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.
Method Purpose String getActionCommand()Returns the string associated with this action. Most objects that can fire action events support a method called setActionCommandthat 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 constantsSHIFT_MASK,CTRL_MASK,META_MASK, andALT_MASKto 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_MASKObject getSource()
(injava.util.EventObject)Returns the object that fired the event.
The following table lists some of the many examples that use action listeners.
Example Where Described Notes BeeperThis section and Introduction to Event Listeners Contains one button with one action listener that beeps when you click the button. MultiListenerIntroduction to Event Listeners Registers two different action listeners on one button. Also registers the same action listener on two different buttons. RadioButtonDemoHow to Use Radio Buttons Registers the same action listener on five radio buttons. The listener uses the getActionCommandmethod to determine which radio button fired the event.MenuDemoHow to Use Menus Shows how to listen for action events on menu items. DragPictureDemo2Adding Cut/Copy/Paste Support to Data Transfer Uses setActionCommandto 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.TextDemoHow to Use Text Fields An applet that registers an action listener on a text field. IconDemoAppletHow to Use Icons Loads an image in an action listener. Because loading an image can take a while, this program uses a SwingWorkerto load the image in a background thread.TableDialogEditDemoHow to Use Tables Registers an action listener through a factory method on the OK button of a color chooser dialog. SliderDemoHow to Use Sliders Registers an action listener on a timer that controls an animation loop.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.