Java examples for Swing:JMenu
Adds a new menu item to the menu with the specified name and attributes.
/**/*from w ww . j a v a2 s .co m*/ // // samskivert library - useful routines for java programs // Copyright (C) 2001-2012 Michael Bayne, et al. // http://github.com/samskivert/samskivert/blob/master/COPYING */ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.lang.reflect.Method; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JPopupMenu; import javax.swing.KeyStroke; import static sg.atom.swing.additionals.util.SwingLog.log; public class Main{ /** * Adds a new menu item to the menu with the specified name and * attributes. * * @param l the action listener. * @param menu the menu to add the item to. * @param name the item name. * @param mnem the mnemonic key for the item or null if none. * @param accel the keystroke for the item or null if none. * * @return the new menu item. */ public static JMenuItem addMenuItem(ActionListener l, JMenu menu, String name, Integer mnem, KeyStroke accel) { JMenuItem item = createItem(name, mnem, accel); item.addActionListener(l); menu.add(item); return item; } /** * Adds a new menu item to the menu with the specified name and * attributes. * * @param l the action listener. * @param menu the menu to add the item to. * @param name the item name. * @param mnem the mnemonic key for the item. * @param accel the keystroke for the item or null if none. * * @return the new menu item. */ public static JMenuItem addMenuItem(ActionListener l, JMenu menu, String name, int mnem, KeyStroke accel) { return addMenuItem(l, menu, name, Integer.valueOf(mnem), accel); } /** * Adds a new menu item to the menu with the specified name and * attributes. * * @param l the action listener. * @param menu the menu to add the item to. * @param name the item name. * @param mnem the mnemonic key for the item. * * @return the new menu item. */ public static JMenuItem addMenuItem(ActionListener l, JMenu menu, String name, int mnem) { return addMenuItem(l, menu, name, Integer.valueOf(mnem), null); } /** * Adds a new menu item to the menu with the specified name. * * @param l the action listener. * @param menu the menu to add the item to. * @param name the item name. * * @return the new menu item. */ public static JMenuItem addMenuItem(ActionListener l, JMenu menu, String name) { return addMenuItem(l, menu, name, null, null); } /** * Adds a new menu item to the menu with the specified name and * attributes. The supplied method name will be called (it must have * the same signature as {@link ActionListener#actionPerformed} but * can be named whatever you like) when the menu item is selected. * * @param menu the menu to add the item to. * @param name the item name. * @param mnem the mnemonic key for the item. * @param accel the keystroke for the item or null if none. * @param target the object on which to invoke a method when the menu is selected. * @param callbackName the name of the method to invoke when the menu is selected. * * @return the new menu item. */ public static JMenuItem addMenuItem(JMenu menu, String name, int mnem, KeyStroke accel, Object target, String callbackName) { JMenuItem item = createItem(name, Integer.valueOf(mnem), accel); item.addActionListener(new ReflectedAction(target, callbackName)); menu.add(item); return item; } /** * Adds a new menu item to the menu with the specified name and * attributes. The supplied method name will be called (it must have * the same signature as {@link ActionListener#actionPerformed} but * can be named whatever you like) when the menu item is selected. * * @param menu the menu to add the item to. * @param name the item name. * @param target the object on which to invoke a method when the menu is selected. * @param callbackName the name of the method to invoke when the menu is selected. * * @return the new menu item. */ public static JMenuItem addMenuItem(JMenu menu, String name, Object target, String callbackName) { JMenuItem item = createItem(name, null, null); item.addActionListener(new ReflectedAction(target, callbackName)); menu.add(item); return item; } /** * Adds a new menu item to the popup menu with the specified name and * attributes. The supplied method name will be called (it must have * the same signature as {@link ActionListener#actionPerformed} but * can be named whatever you like) when the menu item is selected. * * <p> Note that this <code>JPopupMenu</code>-specific implementation * exists solely because <code>JPopupMenu</code> doesn't extend * <code>JMenu</code> and so we have to explicitly call {@link * JPopupMenu#add} rather than {@link JMenu#add}. * * @param menu the menu to add the item to. * @param name the item name. * @param target the object on which to invoke a method when the menu is selected. * @param callbackName the name of the method to invoke when the menu is selected. * * @return the new menu item. */ public static JMenuItem addMenuItem(JPopupMenu menu, String name, Object target, String callbackName) { JMenuItem item = createItem(name, null, null); item.addActionListener(new ReflectedAction(target, callbackName)); menu.add(item); return item; } /** * Creates and configures a menu item. */ protected static JMenuItem createItem(String name, Integer mnem, KeyStroke accel) { JMenuItem item = new JMenuItem(name); if (mnem != null) { item.setMnemonic(mnem.intValue()); } if (accel != null) { item.setAccelerator(accel); } return item; } }