Java JMenu menuItem(JMenu parent, String label, Object... attrs)

Here you can find the source of menuItem(JMenu parent, String label, Object... attrs)

Description

Construct a new JMenuItem then add it to an existing JMenu.

License

Open Source License

Parameter

Parameter Description
parent - the JMenu to add this JMenuItem into (or null if you don't want to add it to any JMenu yet)
label - the text to show on the menu
attrs - a list of attributes to apply onto the new JMenuItem.

Declaration

public static JMenuItem menuItem(JMenu parent, String label,
        Object... attrs) 

Method Source Code

//package com.java2s;
/* Alloy Analyzer 4 -- Copyright (c) 2006-2009, Felix Chang
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *///w  w w . ja  va 2s .  co m

import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.Icon;

import javax.swing.JMenu;

import javax.swing.JMenuItem;

import javax.swing.KeyStroke;

public class Main {
    /** Construct a new JMenuItem then add it to an existing JMenu.
     * @param parent - the JMenu to add this JMenuItem into (or null if you don't want to add it to any JMenu yet)
     * @param label - the text to show on the menu
     * @param attrs - a list of attributes to apply onto the new JMenuItem
     * <p> If one positive number  a is supplied, we call setMnemonic(a)
     * <p> If two positive numbers a and b are supplied, and a!=VK_ALT, and a!=VK_SHIFT, we call setMnemoic(a) and setAccelerator(b)
     * <p> If two positive numbers a and b are supplied, and a==VK_ALT or a==VK_SHIFT, we call setAccelerator(a | b)
     * <p> If an ActionListener is supplied, we call addActionListener(x)
     * <p> If an Boolean x      is supplied, we call setEnabled(x)
     * <p> If an Icon x         is supplied, we call setIcon(x)
     */
    public static JMenuItem menuItem(JMenu parent, String label,
            Object... attrs) {
        JMenuItem m = new JMenuItem(label, null);
        int accelMask = Toolkit.getDefaultToolkit()
                .getMenuShortcutKeyMask();
        boolean hasMnemonic = false;
        for (Object x : attrs) {
            if (x instanceof Character || x instanceof Integer) {
                int k = (x instanceof Character) ? ((int) ((Character) x))
                        : ((Integer) x).intValue();
                if (k < 0)
                    continue;
                if (k == KeyEvent.VK_ALT) {
                    hasMnemonic = true;
                    accelMask = accelMask | InputEvent.ALT_MASK;
                    continue;
                }
                if (k == KeyEvent.VK_SHIFT) {
                    hasMnemonic = true;
                    accelMask = accelMask | InputEvent.SHIFT_MASK;
                    continue;
                }
                if (!hasMnemonic) {
                    m.setMnemonic(k);
                    hasMnemonic = true;
                } else
                    m.setAccelerator(KeyStroke.getKeyStroke(k, accelMask));
            }
            if (x instanceof ActionListener)
                m.addActionListener((ActionListener) x);
            if (x instanceof Icon)
                m.setIcon((Icon) x);
            if (x instanceof Boolean)
                m.setEnabled((Boolean) x);
        }
        if (parent != null)
            parent.add(m);
        return m;
    }
}

Related

  1. getTextByJMenu(List lstMenus)
  2. insertSeparatorIfNeeded(JMenu menu, int position)
  3. isAtLeastOneChildComponentVisible(JMenu menu)
  4. limitMenuSize(JMenu menu, String name, int size)
  5. makeMenu(JMenu menu, List menuItems)
  6. removeAllSeparators(JMenu menu)
  7. removeTopAndBottomSeparators(JMenu menu)
  8. setChildrenEnabled(JMenu menu, boolean enabled)
  9. tearDownMenu(JMenu menu)