Java Swing Menu Item addMenuItem(Container menu, String text, ActionListener listener)

Here you can find the source of addMenuItem(Container menu, String text, ActionListener listener)

Description

Add a new menu item to a given menu

License

Open Source License

Parameter

Parameter Description
menu Menu to add the item to
text Menu item text
listener Menu item's ActionListener or null

Return

Created menu item

Declaration

public static JMenuItem addMenuItem(Container menu, String text, ActionListener listener) 

Method Source Code


//package com.java2s;
/*//from w  ww.  jav  a2  s. c  o  m
 * Copyright 2006-2015 The MZmine 2 Development Team
 * 
 * This file is part of MZmine 2.
 * 
 * MZmine 2 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.
 * 
 * MZmine 2 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
 * MZmine 2; if not, write to the Free Software Foundation, Inc., 51 Franklin
 * St, Fifth Floor, Boston, MA 02110-1301 USA
 */

import java.awt.Container;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;

import javax.swing.KeyStroke;

public class Main {
    /**
     * Add a new menu item to a given menu
     * 
     * @param menu
     *            Menu to add the item to
     * @param text
     *            Menu item text
     * @param listener
     *            Menu item's ActionListener or null
     * @return Created menu item
     */
    public static JMenuItem addMenuItem(Container menu, String text, ActionListener listener) {
        return addMenuItem(menu, text, listener, null, 0, false);
    }

    /**
     * Add a new menu item to a given menu
     * 
     * @param menu
     *            Menu to add the item to
     * @param text
     *            Menu item text
     * @param listener
     *            Menu item's ActionListener or null
     * @param actionCommand
     *            Menu item's action command or null
     * @return Created menu item
     */
    public static JMenuItem addMenuItem(Container menu, String text, ActionListener listener,
            String actionCommand) {
        return addMenuItem(menu, text, listener, actionCommand, 0, false);
    }

    /**
     * Add a new menu item to a given menu
     * 
     * @param menu
     *            Menu to add the item to
     * @param text
     *            Menu item text
     * @param listener
     *            Menu item's ActionListener or null
     * @param mnemonic
     *            Menu item's mnemonic (virtual key code) or 0
     * @return Created menu item
     */
    public static JMenuItem addMenuItem(Container menu, String text, ActionListener listener, int mnemonic) {
        return addMenuItem(menu, text, listener, null, mnemonic, false);
    }

    /**
     * Add a new menu item to a given menu
     * 
     * @param menu
     *            Menu to add the item to
     * @param text
     *            Menu item text
     * @param listener
     *            Menu item's ActionListener or null
     * @param mnemonic
     *            Menu item's mnemonic (virtual key code) or 0
     * @param setAccelerator
     *            Indicates whether to set key accelerator to CTRL + the key
     *            specified by mnemonic parameter
     * @return Created menu item
     */
    public static JMenuItem addMenuItem(Container menu, String text, ActionListener listener, int mnemonic,
            boolean setAccelerator) {
        return addMenuItem(menu, text, listener, null, mnemonic, setAccelerator);
    }

    /**
     * Add a new menu item to a given menu
     * 
     * @param menu
     *            Menu to add the item to
     * @param text
     *            Menu item text
     * @param listener
     *            Menu item's ActionListener or null
     * @param actionCommand
     *            Menu item's action command or null
     * @param mnemonic
     *            Menu item's mnemonic (virtual key code) or 0
     * @param setAccelerator
     *            Indicates whether to set key accelerator to CTRL + the key
     *            specified by mnemonic parameter
     * @return Created menu item
     */
    public static JMenuItem addMenuItem(Container menu, String text, ActionListener listener, String actionCommand,
            int mnemonic, boolean setAccelerator) {
        JMenuItem item = new JMenuItem(text);
        if (listener != null)
            item.addActionListener(listener);
        if (actionCommand != null)
            item.setActionCommand(actionCommand);
        if (mnemonic > 0)
            item.setMnemonic(mnemonic);
        if (setAccelerator)
            item.setAccelerator(KeyStroke.getKeyStroke(mnemonic, ActionEvent.CTRL_MASK));
        if (menu != null)
            menu.add(item);
        return item;
    }
}

Related

  1. addMenuItem(final Action action, final C topLevelMenu, final String... path)
  2. appendMenuItem(Component menuItem, StringBuilder builder, String indent)
  3. createMenu(String menu, String[] menuItemNames)
  4. createMenuItem(Action a)