Java Swing Menu Item createMenuItem(Action a)

Here you can find the source of createMenuItem(Action a)

Description

Creates a JMenuItem from an action.

License

Open Source License

Parameter

Parameter Description
a The action for which to create the menu item.

Return

The newly created menu item.

Declaration

public static JMenuItem createMenuItem(Action a) 

Method Source Code

//package com.java2s;
/*/* ww w  . j a va2 s  . c  o m*/
 * Copyright (C) 2001-2013 Michael Koch (tensberg@gmx.net)
 *
 * This file is part of JGloss.
 *
 * JGloss is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * JGloss is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JGloss; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 */

import javax.swing.Action;

import javax.swing.JMenuItem;

import javax.swing.KeyStroke;

public class Main {
    /**
     * Creates a JMenuItem from an action. All properties from the action, including the
     * accelerator key, will be taken from the action. CAUTION: the created menu item adds a
     * <CODE>propertyChangeListener</CODE> to the action. Thus the item can't be garbage collected
     * while the action object is still alive. If the menu item is no longer used, the action must be
     * removed through a call of <CODE>item.setAction( null)</CODE>.
     *
     * @param a The action for which to create the menu item.
     * @return The newly created menu item.
     */
    public static JMenuItem createMenuItem(Action a) {
        JMenuItem item = new JMenuItem();
        item.setAction(a);
        KeyStroke stroke = (KeyStroke) a.getValue(Action.ACCELERATOR_KEY);
        if (stroke != null) {
            item.setAccelerator(stroke);
        }

        return item;
    }
}

Related

  1. addMenuItem(Container menu, String text, ActionListener listener)
  2. addMenuItem(final Action action, final C topLevelMenu, final String... path)
  3. appendMenuItem(Component menuItem, StringBuilder builder, String indent)
  4. createMenu(String menu, String[] menuItemNames)
  5. createMenuItem(Action action, Icon icon)
  6. createMenuItem(ActionListener listener, String cmd, String toolTip)
  7. createMenuItem(final String label, final String accessibleDescription, final ActionListener actionListener)
  8. createMenuItem(final String text, final ActionListener al, final int mnemonic)