- 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
JMenuItem
contained in a JPopupMenu
performs exactly that function.
Menu items can be configured, and to some degree controlled, by
Action
s. 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
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
JMenuItem.AccessibleJMenuItem
This class implements accessibility support for theJMenuItem
class.Nested classes/interfaces declared in class javax.swing.AbstractButton
AbstractButton.AccessibleAbstractButton, AbstractButton.ButtonChangeListener
Nested classes/interfaces declared in class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Field Summary
Fields declared in class javax.swing.AbstractButton
actionListener, BORDER_PAINTED_CHANGED_PROPERTY, changeEvent, changeListener, CONTENT_AREA_FILLED_CHANGED_PROPERTY, DISABLED_ICON_CHANGED_PROPERTY, DISABLED_SELECTED_ICON_CHANGED_PROPERTY, FOCUS_PAINTED_CHANGED_PROPERTY, HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY, HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY, ICON_CHANGED_PROPERTY, itemListener, MARGIN_CHANGED_PROPERTY, MNEMONIC_CHANGED_PROPERTY, model, MODEL_CHANGED_PROPERTY, PRESSED_ICON_CHANGED_PROPERTY, ROLLOVER_ENABLED_CHANGED_PROPERTY, ROLLOVER_ICON_CHANGED_PROPERTY, ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY, SELECTED_ICON_CHANGED_PROPERTY, TEXT_CHANGED_PROPERTY, VERTICAL_ALIGNMENT_CHANGED_PROPERTY, VERTICAL_TEXT_POSITION_CHANGED_PROPERTY
Fields declared in class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Fields declared in class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
Fields declared in interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Fields declared in interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
-
Constructor Summary
Constructors Constructor Description JMenuItem()
Creates aJMenuItem
with no set text or icon.JMenuItem(String text)
Creates aJMenuItem
with the specified text.JMenuItem(String text, int mnemonic)
Creates aJMenuItem
with the specified text and keyboard mnemonic.JMenuItem(String text, Icon icon)
Creates aJMenuItem
with the specified text and icon.JMenuItem(Action a)
Creates a menu item whose properties are taken from the specifiedAction
.JMenuItem(Icon icon)
Creates aJMenuItem
with the specified icon. -
Method Summary
Modifier and Type Method Description protected void
actionPropertyChanged(Action action, String propertyName)
Updates the button's state in response to property changes in the associated action.void
addMenuDragMouseListener(MenuDragMouseListener l)
Adds aMenuDragMouseListener
to the menu item.void
addMenuKeyListener(MenuKeyListener l)
Adds aMenuKeyListener
to the menu item.protected void
configurePropertiesFromAction(Action a)
Sets the properties on this button to match those in the specifiedAction
.protected void
fireMenuDragMouseDragged(MenuDragMouseEvent event)
Notifies all listeners that have registered interest for notification on this event type.protected void
fireMenuDragMouseEntered(MenuDragMouseEvent event)
Notifies all listeners that have registered interest for notification on this event type.protected void
fireMenuDragMouseExited(MenuDragMouseEvent event)
Notifies all listeners that have registered interest for notification on this event type.protected void
fireMenuDragMouseReleased(MenuDragMouseEvent event)
Notifies all listeners that have registered interest for notification on this event type.protected void
fireMenuKeyPressed(MenuKeyEvent event)
Notifies all listeners that have registered interest for notification on this event type.protected void
fireMenuKeyReleased(MenuKeyEvent event)
Notifies all listeners that have registered interest for notification on this event type.protected void
fireMenuKeyTyped(MenuKeyEvent event)
Notifies all listeners that have registered interest for notification on this event type.KeyStroke
getAccelerator()
Returns theKeyStroke
which serves as an accelerator for the menu item.AccessibleContext
getAccessibleContext()
Returns theAccessibleContext
associated with thisJMenuItem
.Component
getComponent()
Returns thejava.awt.Component
used to paint this object.MenuDragMouseListener[]
getMenuDragMouseListeners()
Returns an array of all theMenuDragMouseListener
s added to this JMenuItem with addMenuDragMouseListener().MenuKeyListener[]
getMenuKeyListeners()
Returns an array of all theMenuKeyListener
s added to this JMenuItem with addMenuKeyListener().MenuElement[]
getSubElements()
This method returns an array containing the sub-menu components for this menu component.String
getUIClassID()
Returns the suffix used to construct the name of the L&F class used to render this component.protected void
init(String text, Icon icon)
Initializes the menu item with the specified text and icon.boolean
isArmed()
Returns whether the menu item is "armed".void
menuSelectionChanged(boolean isIncluded)
Called by theMenuSelectionManager
when theMenuElement
is selected or unselected.protected String
paramString()
Returns a string representation of thisJMenuItem
.void
processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
Processes a key event forwarded from theMenuSelectionManager
and changes the menu selection, if necessary, by usingMenuSelectionManager
's API.void
processMenuDragMouseEvent(MenuDragMouseEvent e)
Handles mouse drag in a menu.void
processMenuKeyEvent(MenuKeyEvent e)
Handles a keystroke in a menu.void
processMouseEvent(MouseEvent e, MenuElement[] path, MenuSelectionManager manager)
Processes a mouse event forwarded from theMenuSelectionManager
and changes the menu selection, if necessary, by using theMenuSelectionManager
's API.void
removeMenuDragMouseListener(MenuDragMouseListener l)
Removes aMenuDragMouseListener
from the menu item.void
removeMenuKeyListener(MenuKeyListener l)
Removes aMenuKeyListener
from the menu item.void
setAccelerator(KeyStroke keyStroke)
Sets the key combination which invokes the menu item's action listeners without navigating the menu hierarchy.void
setArmed(boolean b)
Identifies the menu item as "armed".void
setEnabled(boolean b)
Enables or disables the menu item.void
setUI(MenuItemUI ui)
Sets the look and feel object that renders this component.void
updateUI()
Resets the UI property with a value from the current look and feel.Methods declared in class javax.swing.AbstractButton
addActionListener, addChangeListener, addImpl, addItemListener, checkHorizontalKey, checkVerticalKey, createActionListener, createActionPropertyChangeListener, createChangeListener, createItemListener, doClick, doClick, fireActionPerformed, fireItemStateChanged, fireStateChanged, getAction, getActionCommand, getActionListeners, getChangeListeners, getDisabledIcon, getDisabledSelectedIcon, getDisplayedMnemonicIndex, getHideActionText, getHorizontalAlignment, getHorizontalTextPosition, getIcon,