Using Other Swing Features |
If you have two or more components that perform the same function, consider using anAction
object to implement the function. AnAction
object is anActionListener
that provides not only action-event handling, but also centralized handling of the text, icon, and enabled state of tool bar buttons or menu items. By adding anAction
to aJToolBar
,JMenu
, orJPopupMenu
, you get the following features:Here's an example of using an
- A new
JButton
(forJToolBar
) orJMenuItem
(forJMenu
andJPopupMenu
) that is automatically added to the tool bar or menu. The button or menu item automatically uses the icon and text specified by theAction
.- A registered action listener (the
Action
object) for the button or menu item.- Centralized handling of the button or menu item's enabled state.
Action
to create a tool-bar button and menu item that perform the same function:Action leftAction = new <a class that implements Action>(...); JButton button = toolBar.add(leftAction); JMenuItem menuItem = mainMenu.add(leftAction);For a button or menu item to get the full benefit of using an
Action
, you must create the component using theadd(Action)
method ofJToolBar
,JMenu
, orJPopupMenu
. Currently, no API beyondaddActionListener(ActionListener)
exists to connect anAction
to an already existing component. For example, although you can add anAction
object as an action listener to any button, the button won't be notified when the action is disabled.To create an
Action
object, you generally create a subclass ofAbstractAction
and then instantiate it. In your subclass, you must implement theactionPerformed
method to react appropriately when the action event occurs. Here's an example of creating and instantiating anAbstractAction
subclass:leftAction = new AbstractAction("Go left", new ImageIcon("images/left.gif")) { public void actionPerformed(ActionEvent e) { displayResult("Action for first button/menu item", e); } };Here's a picture of a demo application that uses actions to implement three features.
Here is what the user sees when the "Go left" action is disabled:
Try this:
- Compile and run the application. The source file is
ActionDemo.java
. You will also need three image files:left.gif
,middle.gif
, andright.gif
.
See Getting Started with Swing if you need help.- Choose the top item from the left menu (Menu->Go left).
The text area displays some text identifying both the event source and the action listener that received the event.- Click the leftmost button in the tool bar.
The text area again displays information about the event. Note that although the source of the events is different, both events were detected by the same action listener: theAction
object with which the components were created.- Choose the top item from the Action State menu.
This disables the "Go left"Action
object, which in turn disables its associated menu item and button.
Here's the code that disables the "Go left" action:
After you create components using anboolean selected = ...//true if the action should be enabled; false, otherwise leftAction.setEnabled(selected);Action
, you might well need to customize them. For example, you might want to set the tool-tip text for a button. Or you might want to customize the appearance of one of the components by adding or deleting the icon or text. For example,ActionDemo.java
has no icons in its menus, no text in its buttons, and tool tips for its buttons. Here's the code that accomplishes this:button = toolBar.add(leftAction); button.setText(""); //an icon-only button button.setToolTipText("This is the left button"); menuItem = mainMenu.add(leftAction); menuItem.setIcon(null); //arbitrarily chose not to use icon in menu
The following tables list the commonly usedAction
constructors and methods. The API for usingAction
objects falls into two categories:
Creating and Using an Action Constructor or Method Purpose AbstractAction()
AbstractAction(String)
AbstractAction(String, Icon)Create an Action
object. Through arguments, you can specify the text and icon to be used in the components that the action controls.void setEnabled(boolean)
boolean isEnabled()Set or get whether the components the action controls are enabled. Invoking setEnabled(false)
disables all the components that the action controls. Similarly, invokingsetEnabled(true) enables the action's components.
Creating an Action-Controlled Component Method Purpose JMenuItem add(Action)
JMenuItem insert(Action, int)
(inJMenu
andJPopupMenu
)Create a JMenuItem
object and put it in the menu or popup menu. See the discussion in this section and in How to Use Menus for details.JButton add(Action)
(inJToolBar
)Create a JButton
object and put it in the tool bar. See the discussion in this section and in How to Use Tool Bars for details.
Currently, the only example that usesAction
objects isActionDemo.java
, which is used and described in this section.
Using Other Swing Features |