Sunday, January 23, 2022
HomeGuideHow to Use Actions (The Java™ Tutorials > Creating a GUI With...

How to Use Actions (The Java™ Tutorials > Creating a GUI With Swing > Using Other Swing Features)

Trail: Developing a GUI With Swing

Lesson: Making use of Other Swing Options

The Java Tutorials have been written for JDK eight. Examples and practices described in this web page never take benefit of improvements introduced in later releases and could use technologies no longer offered. See Java Language Adjustments for a summary of updated language functions in Java SE 9 and subsequent releases. See JDK Release Notes for facts about new functions, enhancements, and removed or deprecated possibilities for all JDK releases.

How to Use Actions

An
Action can be applied to separate functionality and state from a element. For instance, if you have two or extra elements that execute the identical function, take into consideration employing an Action object to implement the function. An Action object is an
action listener that offers not only action-occasion handling, but also centralized handling of the state of action-occasion-firing elements such as
tool bar buttons,
menu products,
typical buttons, and
text fields. The state that an action can deal with contains text, icon, mnemonic, enabled, and chosen status.

You normally attach an action to a element employing the setAction system. Here’s what takes place when setAction is invoked on a element:

  • The component’s state is updated to match the state of the Action. For instance, if the Action‘s text and icon values had been set, the component’s text and icon are set to these values.
  • The Action object is registered as an action listener on the element.
  • If the state of the Action adjustments, the component’s state is updated to match the Action. For instance, if you transform the enabled status of the action, all elements it really is attached to transform their enabled states to match the action.

Here’s an instance of building a tool-bar button and menu item that execute the identical function:

Action leftAction = new LeftAction() //LeftAction code is shown later
...
button = new JButton(leftAction)
...
menuItem = new JMenuItem(leftAction)

To develop an Action object, you normally develop a subclass of
AbstractAction and then instantiate it. In your subclass, you ought to implement the actionPerformed system to react appropriately when the action occasion happens. Here’s an instance of building 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(Brief_DESCRIPTION, desc)
        putValue(MNEMONIC_Essential, mnemonic)
    }
    public void actionPerformed(ActionEvent e) {
        displayResult("Action for first button/menu item", e)
    }
}

When the action developed by the preceding code is attached to a button and a menu item, the button and menu item show the text and icon linked with the action. The L character is applied for mnemonics on the button and menu item, and their tool-tip text is set to the Brief_DESCRIPTION string followed by a representation of the mnemonic essential.

See also  Amumu Aram – Abis Labs
See also  Alistair

For instance, we have offered a basic instance,
ActionDemo.java, which defines 3 actions. Each and every action is attached to a button and a menu item. Thanks to the mnemonic values set for each and every button’s action, the essential sequence Alt-L activates the left button, Alt-M the middle button, and Alt-R the correct button. The tool tip for the left button displays This is the left button. Alt-L. All of this configuration happens automatically, without having the system creating explicit calls to set the mnemonic or tool-tip text. As we’ll show later, the system does make calls to set the button text, but only to keep away from employing the values currently set by the actions.

A snapshot of ActionDemo, which uses actions to coordinate menus and buttons.Attempt this: 

  1. Click the Launch button to run ActionDemo employing
    Java™ Internet Get started (download JDK 7 or later). Or, to compile and run the instance oneself, seek advice from the instance index.

    Launches the ActionDemo example

  2. Opt for the best item from the left menu (Menu &gt Go left).
    The text location displays some text identifying each the occasion supply and the action listener that received the occasion.

  3. Click the leftmost button in the tool bar.
    The text location once more displays facts about the occasion. Note that while the supply of the events is distinctive, each events had been detected by the identical action listener: the Action object attached to the elements.

  4. Opt for the best item from the Action State menu.
    This disables the “Go left” Action object, which in turn disables its linked menu item and button.

Right here is what the user sees when the “Go left” action is disabled:

A snapshot of ActionDemo when
A snapshot of ActionDemo when

Here’s the code that disables the “Go left” action:

boolean chosen = ...//accurate if the action ought to be enabled
                      //false, otherwise
leftAction.setEnabled(chosen)

Right after you develop elements employing an Action, you could nicely require to customize them. For instance, you could want to customize the look of one particular of the elements by adding or deleting the icon or text. For instance,
ActionDemo.java 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 develop an icon-only button and a text-only menu item from the identical action by setting the icon home to null and the text to an empty string. Even so, if a home of the Action adjustments, the widget may possibly attempt to reset the icon and text from the Action once more.

The following tables list the typically applied Action constructors and techniques. The API for employing Action objects falls into 3 categories:

Action Properties

This table defines the properties that can be set on an action. The second column lists which elements automatically use the properties (and what system is particularly named). For instance, setting the ACCELERATOR_Essential on an action that is then attached to a menu item, indicates that JMenuItem.setAccelerator(KeyStroke) is named automatically.

House
Auto-Applied to:
Class
(Process Named)
Objective

ACCELERATOR_Essential
JMenuItem
(setAccelerator)
The KeyStroke to be applied as the accelerator for the action. For a discussion of accelerators versus mnemonics, see
Enabling Keyboard Operation.
ACTION_COMMAND_Essential
AbstractButton, JCheckBox, JRadioButton
(setActionCommand)
The command string linked with the ActionEvent.

Lengthy_DESCRIPTION
None
The longer description for the action. Can be applied for context-sensitive aid.

MNEMONIC_Essential
AbstractButton, JMenuItem, JCheckBox, JRadioButton
(setMnemonic)
The mnemonic for the action. For a discussion of accelerators versus mnemonics, see
Enabling Keyboard Operation.
NAME
AbstractButton, JMenuItem, JCheckBox, JRadioButton
(setText)
The name of the action. You can set this home when building the action employing the AbstractAction(String) or AbstractAction(String, Icon) constructors.

Brief_DESCRIPTION
AbstractButton, JCheckBox, JRadioButton
(setToolTipText)
The brief description of the action.

Modest_ICON
AbstractButton, JMenuItem
(setIcon)
The icon for the action applied in the tool bar or on a button. You can set this home when building the action employing the AbstractAction(name, icon) constructor.

The following examples use Action objects.

Instance
Exactly where Described
Notes

ActionDemo
This section
Makes use of actions to bind buttons and menu products to the identical function.

TextComponentDemo
Text Element Options
Makes use of text actions to develop menu products for text editing commands, such as reduce, copy, and paste, and to bind essential strokes to caret movement. Also implements custom AbstractAction subclasses to implement undo and redo. The text action discussion starts in Ideas: About Editor Kits.

See also  10 Best Stun Champions in League of Legends
RELATED ARTICLES

Most Popular

Recent Comments