Java Swing Menu Item createMenuItem(String name, String description, int mnemonic, int accelerator, ActionListener listener)

Here you can find the source of createMenuItem(String name, String description, int mnemonic, int accelerator, ActionListener listener)

Description

Makes a new menu item.

License

Open Source License

Parameter

Parameter Description
name The name of the menu item.
description The accessible description.
mnemonic The key (when pressed with Alt) that activates the menu item.
accelerator The shortcut key sequence that activates the menu item.
listener The action listener.

Return

A properly-initialized new menu item.

Declaration

public static JMenuItem createMenuItem(String name, String description, int mnemonic, int accelerator,
        ActionListener listener) 

Method Source Code

//package com.java2s;
/**//  ww w. ja v  a  2 s .com
 * SwingHelper.java
 * Handles the loading repetitive code involved in Swing control management.
 * 
 * Copyright (c) 2012 The Javanauts
 *
 * 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.
 */

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

import javax.swing.JMenuItem;

import javax.swing.KeyStroke;

public class Main {
    /**
     * Makes a new menu item.
     * 
     * @param name
     *            The name of the menu item.
     * @param description
     *            The accessible description.
     * @param mnemonic
     *            The key (when pressed with Alt) that activates the menu item.
     * @param accelerator
     *            The shortcut key sequence that activates the menu item.
     * @param listener
     *            The action listener.
     * @return A properly-initialized new menu item.
     */
    public static JMenuItem createMenuItem(String name, String description, int mnemonic, int accelerator,
            ActionListener listener) {
        JMenuItem item = new JMenuItem(name, mnemonic);
        item.getAccessibleContext().setAccessibleDescription(description);
        item.setAccelerator(
                KeyStroke.getKeyStroke(accelerator, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
        item.addActionListener(listener);
        return item;
    }
}

Related

  1. createMenuItem(final String text, final Icon icon)
  2. createMenuItem(String caption, Action action)
  3. createMenuItem(String menuItemName, ActionListener actionListener)
  4. createMenuItem(String menuText, ActionListener listener)
  5. createMenuitem(String name, ActionListener listener)
  6. createMenuItem(String strTitle, String strActionCommand, ActionListener alListener)
  7. createMenuItem(String text, boolean visible)
  8. createMenuItem(String text, Icon icon, String toolTip, ActionListener... listeners)
  9. createMenuItem(T source, int mnemonic, String description, E action)