< Day Day Up > |
If you have two or more components that perform the same function, consider using an Actio n [1] object to implement the function. An Action object is an action listener [2] that provides not only action-event handling but also centralized handling of the state of action-event-firing components such as tool-bar buttons, menu items, common buttons , and text fields. The states that an action can handle include text, icon, mnemonic, and enabled status.
You typically attach an action to a component using the setAction method. Here's what happens when setAction is invoked on a component:
Here's an example of creating a tool-bar button and menu item that perform the same function: Action leftAction = new LeftAction(); //LeftAction code is shown later ... button = new JButton(leftAction) ... menuItem = new JMenuItem(leftAction); Version Note: Prior to the v1.3 release of the Java 2 platform, the only way for a button or menu item to get the full benefit of an Action was to create the component using the add(Action) method of JToolBar , JMenu , or JPopupMenu . This was because the pre-1.3 releases had no API except addActionListener(ActionListener) to connect an Action to an already existing component. Although you could use addActionListener to add an Action object as an action listener to any button, for example, the button wouldn't be notified when the action was disabled. To create an Action object, you generally create a subclass of AbstractAction [3] and instantiate it. In your subclass, you must implement the actionPerformed method to react appropriately when the action event occurs.
Here's an example of creating and instantiating an AbstractAction subclass: leftAction = new LeftAction("Go left", anIcon, "This is the left button.", new Integer(KeyEvent.VK_L)); ... class LeftAction extends AbstractAction { public LeftAction(String text, ImageIcon icon, String desc, Integer mnemonic) { super(text, icon); putValue(SHORT_DESCRIPTION, desc); putValue(MNEMONIC_KEY, mnemonic); } public void actionPerformed(ActionEvent e) { displayResult("Action for first button/menu item", e); } } When the action created by this code is attached to a button and a menu item, the button and menu item display the text and icon associated with the action. The L character is used for mnemonics on the button and menu item, and their tool-tip text is set to the SHORT_DESCRIPTION string followed by a representation of the mnemonic key. We've provided a simple example, ActionDemo , which defines three actions, each attached to a button and a menu item. (See Figure 1.) Thanks to the mnemonic values set for each button's action, the key sequence Alt-L activates the left button, Alt-M the middle button, and Alt-R the right button. The tool tip for the left button displays "This is the left button. Alt-L." All of this configuration occurs automatically, without the program having to make explicit calls to set the mnemonic or the tool-tip text. As we'll show later, the program does make calls to set the button text, but only to avoid using the values already set by the actions. Figure 1. A screenshot of the ActionDemo example.
Try This:
Here's the code that disables the Go left action: boolean selected = ...//true if the action should be enabled; //false, otherwise leftAction.setEnabled(selected); After you create components using an Action , you might need to customize them. For example, you might want to customize the appearance of one of the components by adding or deleting the icon or text. ActionDemo has no icons in its menus and no text in its buttons. Here's the code that accomplishes this: menuItem = new JMenuItem(); menuItem.setAction(leftAction); menuItem.setIcon(null); //arbitrarily chose not to use icon in menu ... button = new JButton(); button.setAction(leftAction); button.setText(""); //an icon-only button We chose to create an icon-only button and a text-only menu item from the same action by setting the icon property to null and the text to an empty string. However, if a property of the Action changes, the widget may try again to reset the icon and text from the Action . The Action APITables 1 and 2 list the commonly used Action constructors and methods . You can find the Action API documentation online at: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/Action.html. Table 1. Components That Support set/getAction
Table 2. Creating and Using an AbstractAction
Table 3 defines the properties that can be set on an action. The second column lists which components automatically use the properties (the method specifically called). For example, setting the ACCELERATOR_KEY on an action that is then attached to a menu item means that JMenuItem.setAccelerator(KeyStroke) is called automatically. Table 3. Action Properties
Examples That Use ActionsThe following examples use Action objects.
|
< Day Day Up > |