Module java.desktop
Package javax.swing

Class JMenuItem

All Implemented Interfaces:
ImageObserver, ItemSelectable, MenuContainer, Serializable, Accessible, MenuElement, SwingConstants
Direct Known Subclasses:
JCheckBoxMenuItem, JMenu, JRadioButtonMenuItem

@JavaBean(defaultProperty="UIClassID",
          description="An item which can be selected in a menu.")
public class JMenuItem
extends AbstractButton
implements Accessible, MenuElement
An implementation of an item in a menu. A menu item is essentially a button sitting in a list. When the user selects the "button", the action associated with the menu item is performed. A JMenuItem contained in a JPopupMenu performs exactly that function.

Menu items can be configured, and to some degree controlled, by Actions. Using an Action with a menu item has many benefits beyond directly configuring a menu item. Refer to Swing Components Supporting Action for more details, and you can find more information in How to Use Actions, a section in The Java Tutorial.

For further documentation and for examples, see How to Use Menus in The Java Tutorial.

Warning: Swing is not thread safe. For more information see Swing's Threading Policy.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans™ has been added to the java.beans package. Please see XMLEncoder.

Since:
1.2
See Also:
JPopupMenu, JMenu, JCheckBoxMenuItem, JRadioButtonMenuItem
  • Constructor Details

    • JMenuItem

      public JMenuItem()
      Creates a JMenuItem with no set text or icon.
    • JMenuItem

      public JMenuItem​(Icon icon)
      Creates a JMenuItem with the specified icon.
      Parameters:
      icon - the icon of the JMenuItem
    • JMenuItem

      public JMenuItem​(String text)
      Creates a JMenuItem with the specified text.
      Parameters:
      text - the text of the JMenuItem
    • JMenuItem

      public JMenuItem​(Action a)
      Creates a menu item whose properties are taken from the specified Action.
      Parameters:
      a - the action of the JMenuItem
      Since:
      1.3
    • JMenuItem

      public JMenuItem​(String text, Icon icon)
      Creates a JMenuItem with the specified text and icon.
      Parameters:
      text - the text of the JMenuItem
      icon - the icon of the JMenuItem
    • JMenuItem

      public JMenuItem​(String text, int mnemonic)
      Creates a JMenuItem with the specified text and keyboard mnemonic.
      Parameters:
      text - the text of the JMenuItem
      mnemonic - the keyboard mnemonic for the JMenuItem
  • Method Details