Java JMenu addMenuItem(ActionListener li, JMenu menu, String text, String tip)

Here you can find the source of addMenuItem(ActionListener li, JMenu menu, String text, String tip)

Description

add a new item to the give menu, add an action listener and set the tool tip

License

Open Source License

Parameter

Parameter Description
li a parameter
menu a parameter
text a parameter
tip a parameter

Return

the new JMenuItem

Declaration

public static JMenuItem addMenuItem(ActionListener li, JMenu menu, String text, String tip) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

public class Main {
    /**//from   w  w  w  . j  av  a  2s .  c  o m
     * add a new item to the give menu and add an action listener
     * 
     * @param li
     * @param menu
     * @param text
     * @return
     */
    public static JMenuItem addMenuItem(ActionListener li, JMenu menu, String text) {
        JMenuItem mi = new JMenuItem(text);
        mi.addActionListener(li);
        menu.add(mi);
        return mi;
    }

    /**
     * add a new item to the give menu, add an action listener and set the tool
     * tip
     * 
     * @param li
     * @param menu
     * @param text
     * @param tip
     * @return the new JMenuItem
     */
    public static JMenuItem addMenuItem(ActionListener li, JMenu menu, String text, String tip) {
        JMenuItem mi = new JMenuItem(text);
        mi.addActionListener(li);
        mi.setToolTipText(tip);
        menu.add(mi);
        return mi;
    }

    /**
     * add a new item to the give menu, add an action listener, set the tool tip
     * and set the keyboard accelerator
     * 
     * @param li
     * @param menu
     * @param text
     * @param tip
     * @param key
     * @return
     */
    public static JMenuItem addMenuItem(ActionListener li, JMenu menu, String text, String tip, String key) {
        JMenuItem mi = addMenuItem(li, menu, text, tip);
        mi.setAccelerator(KeyStroke.getKeyStroke(key));
        return mi;
    }
}

Related

  1. addListenerToItems(JMenu menu, ActionListener listener)
  2. addMenuItem(ActionListener al, JMenu m, String label, String command, int key, int ckey)
  3. addMenuItem(JMenu menu, Action action)
  4. addMenuItem(JMenu menu, String label, Action action, int mnemonic, String tooltip, boolean isEnabled)
  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)