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;

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.
     */// w ww.ja va  2s  .c  om
    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.toString(), 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)