Java JMenu addMenuItem(JMenu menu, String label, Action action, int mnemonic, String tooltip, boolean isEnabled)

Here you can find the source of addMenuItem(JMenu menu, String label, Action action, int mnemonic, String tooltip, boolean isEnabled)

Description

Add an action to a menu and return the menu item created.

License

Open Source License

Declaration

public static JMenuItem addMenuItem(JMenu menu, String label,
        Action action, int mnemonic, String tooltip, boolean isEnabled) 

Method Source Code

//package com.java2s;
/*//from   w ww.  ja v a  2 s . co m
 Copyright (c) 1998-2013 The Regents of the University of California
 All rights reserved.
 Permission is hereby granted, without written agreement and without
 license or royalty fees, to use, copy, modify, and distribute this
 software and its documentation for any purpose, provided that the above
 copyright notice and the following two paragraphs appear in all copies
 of this software.

 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 PROVIDED HEREUNDER IS ON AN  BASIS, AND THE UNIVERSITY OF
 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 ENHANCEMENTS, OR MODIFICATIONS.

 PT_COPYRIGHT_VERSION_2
 COPYRIGHTENDKEY
 */

import javax.swing.Action;

import javax.swing.JMenu;
import javax.swing.JMenuItem;

import javax.swing.KeyStroke;

public class Main {
    /** JDK1.2 doesn't have this string defined in javax.swing.Action.
     *  This is the value that JDK1.3 uses.
     */
    public static final String ACCELERATOR_KEY = "AcceleratorKey";
    /** JDK1.2 doesn't have this string defined in javax.swing.Action.
     *  This is the value that JDK1.3 uses.
     */
    public static final String MNEMONIC_KEY = "MnemonicKey";

    /** Add an action to a menu and return the menu item created.  If
     * the tool tip is null, use the "tooltip" property already in the
     * action, otherwise add the property to the action. (The mnemonic
     * isn't added.)  The new menu item is added to the action as the
     * "menuItem" property.  The menu item's text is set using the
     * action's name, concatenated with a description of a keyboard
     * accelerator, if one has been set previously on the action.
     * The item will be enabled by default.
     */
    public static JMenuItem addMenuItem(JMenu menu, Action action) {
        String label = (String) action.getValue(Action.NAME);
        int mnemonic = 0;
        Integer i = (Integer) action.getValue(MNEMONIC_KEY);

        if (i != null) {
            mnemonic = i.intValue();
        }

        return addMenuItem(menu, label, action, mnemonic, null, true);
    }

    /** Add an action to a menu and return the menu item created.  If
     * the tool tip is null, use the "tooltip" property already in the
     * action, otherwise add the property to the action. (The mnemonic
     * isn't added.)  The new menu item is added to the action as the
     * "menuItem" property.  The menu item's text is set using the
     * action's name, concatenated with a description of a keyboard
     * accelerator, if one has been set previously on the action.
     * The item will be enabled by default.
     */
    public static JMenuItem addMenuItem(JMenu menu, Action action,
            int mnemonic, String tooltip) {
        String label = (String) action.getValue(Action.NAME);
        return addMenuItem(menu, label, action, mnemonic, tooltip, true);
    }

    /** Add an action to a menu and return the menu item created.  If
     * the tool tip is null, use the "tooltip" property already in the
     * action, otherwise add the property to the action. (The mnemonic
     * isn't added.)  The new menu item is added to the action as the
     * "menuItem" property.  The menu item's text is set to be "label",
     * and is disabled or enabled according to "isEnabled."
     */
    public static JMenuItem addMenuItem(JMenu menu, String label,
            Action action, int mnemonic, String tooltip, boolean isEnabled) {
        if (tooltip == null) {
            tooltip = (String) action.getValue("tooltip");
        } else {
            action.putValue("tooltip", tooltip);
        }

        action.putValue("tooltip", tooltip);

        JMenuItem item = menu.add(action);
        item.setText(label);
        item.setEnabled(isEnabled);
        item.setMnemonic(mnemonic);
        item.setToolTipText(tooltip);

        KeyStroke key = (KeyStroke) action.getValue(ACCELERATOR_KEY);
        item.setAccelerator(key);
        action.putValue("menuItem", item);
        return item;
    }
}

Related

  1. addListenerToItems(JMenu menu, ActionListener listener)
  2. addMenuItem(ActionListener al, JMenu m, String label, String command, int key, int ckey)
  3. addMenuItem(ActionListener li, JMenu menu, String text, String tip)
  4. addMenuItem(JMenu menu, Action action)
  5. addMenuItem(JMenu menu, String label, Action action, int mnemonic, String tooltip, boolean isEnabled)
  6. addMenuItem(Window window, JMenu menu, String title, String icon, int mnemonic, KeyStroke key, ActionListener listener)
  7. addMenuItems(JMenu menu, JComponent... items)
  8. addRadioButtonMenuItem(ActionListener al, JMenu m, String label, String command, boolean b, ButtonGroup g)
  9. addSeparator(JMenu menu)