The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > Actions  [3 examples]

e855. Creating an Action

An action is used by a Swing component to invoke a method. To create an action, the actionPerformed() method must be overridden. The action is then attached to a component such as a button or bound to a keystroke in a text component. When the button is activated or the keystroke is pressed, the action's actionPerformed() method is called. Actions can be attached to more than one component or keystroke.

Actions can also contain other optional information, such as a label, icon, or tool tip text. When the action is attached to a component, the component may use this information if present. For example, if the action has a label and icon, a button created using that action will use that label and icon.

This example defines an action and creates a button using the action.

    // Create an action object
    public Action action = new AbstractAction("Action Name") {
        // This is an instance initializer; it is executed just after the
        // constructor of the superclass is invoked
        {
            // The following values are completely optional
    
            // Set tool tip text
            putValue(Action.SHORT_DESCRIPTION, "Tool Tip Text");
    
            // This text is not directly used by any Swing component;
            // however, this text could be used in a help system
            putValue(Action.LONG_DESCRIPTION, "Context-Sensitive Help Text");
    
            // Set an icon
            Icon icon = new ImageIcon("icon.gif");
            putValue(Action.SMALL_ICON, icon);
    
            // Set a mnemonic character. In most look and feels, this causes the
            // specified character to be underlined This indicates that if the component
            // using this action has the focus and In some look and feels, this causes
            // the specified character in the label to be underlined and
            putValue(Action.MNEMONIC_KEY, new Integer(java.awt.event.KeyEvent.VK_A));
    
            // Set an accelerator key; this value is used by menu items
            putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control F2"));
        }
    
        // This method is called when the action is invoked
        public void actionPerformed(ActionEvent evt) {
            // Perform action
        }
    };
Create a button using the action object:
    JButton button = new JButton(action);

 Related Examples
e856. Listing the Actions in a Component
e857. Enabling an Action

See also: JButton    JCheckBox    JComboBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JLabel    JList    JProgressBar    JRadioButton    JScrollPane    JSlider    JSpinner    JSplitPane    JTabbedPane    JToolBar    Keystrokes and Input Maps    Layout    Look and Feel    Menus    Progress Monitor    The Screen    Tool Tips    UI Default Values   


© 2002 Addison-Wesley.