java.lang.Object | +--java.awt.Component | +--java.awt.Container | +--javax.swing.JComponent | +--javax.swing.AbstractButton | +--javax.swing.JMenuItem
Safe: 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.
For further documentation and for examples, see
How to Use Menus
in The Java Tutorial.
For the keyboard keys used by this component in the standard Look and
Feel (L&F) renditions, see the
JMenuItem key assignments.
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 JavaBeansTM
has been added to the java.beans package.
Please see java.beans.XMLEncoder.
JPopupMenu,
JMenu,
JCheckBoxMenuItem,
JRadioButtonMenuItem,
Serialized FormactionListener, 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
_bounds, accessibleContext, listenerList, paintingChild, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
JMenuItem(Action a)
Action.
JMenuItem(String text,
int mnemonic)
JMenuItem with the specified text and
keyboard mnemonic.
void
addMenuDragMouseListener(MenuDragMouseListener l)
MenuDragMouseListener to the menu item.
(package private) boolean
alwaysOnTop()
Menus, by definition,
should always be on top of all other windows.
protected void
configurePropertiesFromAction(Action a)
ActionEvent source's
properties according to values from the Action instance.
protected java.beans.PropertyChangeListener
createActionPropertyChangeListener(Action a)
PropertyChangeListener
used to update the ActionEvent source as properties
change on its Action instance.
protected void
fireMenuDragMouseDragged(MenuDragMouseEvent event)
protected void
fireMenuDragMouseEntered(MenuDragMouseEvent event)
protected void
fireMenuDragMouseExited(MenuDragMouseEvent event)
protected void
fireMenuDragMouseReleased(MenuDragMouseEvent event)
protected void
fireMenuKeyPressed(MenuKeyEvent event)
protected void
fireMenuKeyReleased(MenuKeyEvent event)
protected void
fireMenuKeyTyped(MenuKeyEvent event)
KeyStroke
getAccelerator()
KeyStroke which serves as an accelerator
for the menu item.
AccessibleContext
getAccessibleContext()
AccessibleContext associated with this
JMenuItem.
MenuDragMouseListener[]
getMenuDragMouseListeners()
MenuDragMouseListeners added
to this JMenuItem with addMenuDragMouseListener().
MenuKeyListener[]
getMenuKeyListeners()
MenuKeyListeners added
to this JMenuItem with addMenuKeyListener().
MenuElement[]
getSubElements()
String
getUIClassID()
protected void
init(String text,
Icon icon)
void
menuSelectionChanged(boolean isIncluded)
MenuSelectionManager when the
MenuElement is selected or unselected.
void
processKeyEvent(KeyEvent e,
MenuElement[] path,
MenuSelectionManager manager)
MenuSelectionManager and changes the menu selection,
if necessary, by using MenuSelectionManager's API.
void
processMouseEvent(MouseEvent e,
MenuElement[] path,
MenuSelectionManager manager)
MenuSelectionManager and changes the menu
selection, if necessary, by using the
MenuSelectionManager's API.
private void
readObject(ObjectInputStream s)
void
removeMenuDragMouseListener(MenuDragMouseListener l)
MenuDragMouseListener from the menu item.
void
removeMenuKeyListener(MenuKeyListener l)
MenuKeyListener from the menu item.
void
setAccelerator(KeyStroke keyStroke)
void
setUI(javax.swing.plaf.MenuItemUI ui)
private void
writeObject(ObjectOutputStream s)
JComponent to an
ObjectOutputStream we temporarily uninstall its UI.
addActionListener, addChangeListener, addItemListener, checkHorizontalKey, checkVerticalKey, configurePropertiesFromAction, createActionListener, createChangeListener, createItemListener, doClick, doClick, fireActionPerformed, fireItemStateChanged, fireStateChanged, getAction, getActionCommand, getActionListeners, getChangeListeners, getDisabledIcon, getDisabledSelectedIcon, getDisplayedMnemonicIndex, getHorizontalAlignment, getHorizontalTextPosition, getIcon, getIconTextGap, getItemListeners, getLabel, getMargin, getMnemonic, getModel, getMultiClickThreshhold, getPressedIcon, getRolloverIcon, getRolloverSelectedIcon, getSelectedIcon, getSelectedObjects, getText, getUI, getVerticalAlignment, getVerticalTextPosition, imageUpdate, isBorderPainted, isContentAreaFilled, isFocusPainted, isRolloverEnabled, isSelected, paintBorder, removeActionListener, removeChangeListener, removeItemListener, setAction, setActionCommand, setBorderPainted, setContentAreaFilled, setDisabledIcon, setDisabledSelectedIcon, setDisplayedMnemonicIndex, setFocusPainted, setHorizontalAlignment, setHorizontalTextPosition, setIcon, setIconTextGap, setLabel, setMargin, setMnemonic, setMnemonic, setModel, setMultiClickThreshhold, setPressedIcon, setRolloverEnabled, setRolloverIcon, setRolloverSelectedIcon, setSelected, setSelectedIcon, setText, setUI, setVerticalAlignment, setVerticalTextPosition
_paintImmediately, addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, checkIfChildObscuredBySibling, componentInputMapChanged, computeVisibleRect, computeVisibleRect, compWriteObjectNotify, contains, createToolTip, disable, enable, enableSerialization, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getCreatedDoubleBuffer, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getManagingFocusBackwardTraversalKeys, getManagingFocusForwardTraversalKeys, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getWriteObjCounter, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyBindings, processKeyBindingsForAllComponents, processKeyEvent, processMouseMotionEvent, putClientProperty, rectangleIsObscured, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setCreatedDoubleBuffer, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPaintingChild, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, setWriteObjCounter, shouldDebugGraphics, superProcessMouseMotionEvent, unregisterKeyboardAction, update
add, add, add, add, add, addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, get, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, iterate, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, size, transferFocusBackward, transferFocusDownCycle, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, toString, transferFocus, transferFocus, transferFocusUpCycle
private static final String uiClassID
private static final boolean TRACE
private static final boolean VERBOSE
private static final boolean DEBUG
private boolean isMouseDragged
private KeyStroke accelerator
public JMenuItem()
JMenuItem with no set text or icon.
public JMenuItem(Icon icon)
JMenuItem with the specified icon.
icon - the icon of the JMenuItempublic JMenuItem(String text)
JMenuItem with the specified text.
text - the text of the JMenuItempublic JMenuItem(Action a)
Action.
a - the action of the JMenuItempublic JMenuItem(String text, Icon icon)
JMenuItem with the specified text and icon.
text - the text of the JMenuItemicon - the icon of the JMenuItempublic JMenuItem(String text, int mnemonic)
JMenuItem with the specified text and
keyboard mnemonic.
text - the text of the JMenuItemmnemonic - the keyboard mnemonic for the JMenuItemvoid initFocusability()
JMenuItem.
JMenuItem's are focusable, but subclasses may
want to be, this provides them the opportunity to override this
and invoke something else, or nothing at all. Refer to
javax.swing.JMenu#initFocusability for the motivation of
this.
protected void init(String text, Icon icon)
init in class AbstractButtontext - the text of the JMenuItemicon - the icon of the JMenuItempublic void setUI(javax.swing.plaf.MenuItemUI ui)
ui - the JMenuItemUI L&F objectUIDefaults.getUI(javax.swing.JComponent)public void updateUI()
updateUI in class AbstractButtonJComponent.updateUI()public String getUIClassID()
getUIClassID in class JComponentJComponent.getUIClassID(),
UIDefaults.getUI(javax.swing.JComponent)public void setArmed(boolean b)
b - true to arm the menu item so it can be selectedpublic boolean isArmed()
setArmed(boolean)public void setEnabled(boolean b)
setEnabled in class AbstractButtonb - true to enable the itemboolean alwaysOnTop()
Menus, by definition,
should always be on top of all other windows. If the menu is
in an internal frame false is returned due to the rollover effect
for windows laf where the menu is not always on top.
alwaysOnTop in class JComponentpublic void setAccelerator(KeyStroke keyStroke)
keyStroke - the KeyStroke which will
serve as an acceleratorpublic KeyStroke getAccelerator()
KeyStroke which serves as an accelerator
for the menu item.
KeyStroke object identifying the
accelerator keyprotected void configurePropertiesFromAction(Action a)
ActionEvent source's
properties according to values from the Action instance.
The properties which are set may differ for subclasses.
By default, this method sets the same properties as
AbstractButton.configurePropertiesFromAction(), plus
Accelerator.
configurePropertiesFromAction in class AbstractButtona - the Action from which to get the properties,
or nullActionprotected java.beans.PropertyChangeListener createActionPropertyChangeListener(Action a)
PropertyChangeListener
used to update the ActionEvent source as properties
change on its Action instance.
Subclasses may override this in order to provide their own
PropertyChangeListener if the set of
properties which should be kept up to date differs.
Note that PropertyChangeListeners should avoid holding
strong references to the ActionEvent source,
as this may hinder garbage collection of the ActionEvent
source and all components in its containment hierarchy.
createActionPropertyChangeListener in class AbstractButtona - the Action from which to get the properties,
or nullActionpublic void processMouseEvent(MouseEvent e, MenuElement[] path, MenuSelectionManager manager)
MenuSelectionManager and changes the menu
selection, if necessary, by using the
MenuSelectionManager's API.
Note: you do not have to forward the event to sub-components.
This is done automatically by the MenuSelectionManager.
processMouseEvent in interface MenuElemente - a MouseEventpath - the MenuElement path arraymanager - the MenuSelectionManagerpublic void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
MenuSelectionManager and changes the menu selection,
if necessary, by using MenuSelectionManager's API.
Note: you do not have to forward the event to sub-components.
This is done automatically by the MenuSelectionManager.
processKeyEvent in interface MenuElemente - a KeyEventpath - the MenuElement path arraymanager - the MenuSelectionManagerpublic void processMenuDragMouseEvent(MenuDragMouseEvent e)
e - a MenuDragMouseEvent objectpublic void processMenuKeyEvent(MenuKeyEvent e)
e - a MenuKeyEvent objectprotected void fireMenuDragMouseEntered(MenuDragMouseEvent event)
event - a MenuMouseDragEventEventListenerListprotected void fireMenuDragMouseExited(MenuDragMouseEvent event)
event - a MenuDragMouseEventEventListenerListprotected void fireMenuDragMouseDragged(MenuDragMouseEvent event)
event - a MenuDragMouseEventEventListenerListprotected void fireMenuDragMouseReleased(MenuDragMouseEvent event)
event - a MenuDragMouseEventEventListenerListprotected void fireMenuKeyPressed(MenuKeyEvent event)
EventListenerListprotected void fireMenuKeyReleased(MenuKeyEvent event)
EventListenerListprotected void fireMenuKeyTyped(MenuKeyEvent event)
event - a MenuKeyEventEventListenerListpublic void menuSelectionChanged(boolean isIncluded)
MenuSelectionManager when the
MenuElement is selected or unselected.
menuSelectionChanged in interface MenuElementisIncluded - true if this menu item is on the part of the menu
path that changed, false if this menu is part of the
a menu path that changed, but this particular part of
that path is still the sameMenuSelectionManager.setSelectedPath(MenuElement[])public MenuElement[] getSubElements()
getSubElements in interface MenuElementMenuElementspublic Component getComponent()
java.awt.Component used to paint
this object. The returned component will be used to convert
events and detect if an event is inside a menu component.
getComponent in interface MenuElementComponent that paints this menu itempublic void addMenuDragMouseListener(MenuDragMouseListener l)
MenuDragMouseListener to the menu item.
l - the MenuDragMouseListener to be addedpublic void removeMenuDragMouseListener(MenuDragMouseListener l)
MenuDragMouseListener from the menu item.
l - the MenuDragMouseListener to be removedpublic MenuDragMouseListener[] getMenuDragMouseListeners()
MenuDragMouseListeners added
to this JMenuItem with addMenuDragMouseListener().
MenuDragMouseListeners added or an empty
array if no listeners have been addedpublic void addMenuKeyListener(MenuKeyListener l)
MenuKeyListener to the menu item.
l - the MenuKeyListener to be addedpublic void removeMenuKeyListener(MenuKeyListener l)
MenuKeyListener from the menu item.
l - the MenuKeyListener to be removedpublic MenuKeyListener[] getMenuKeyListeners()
MenuKeyListeners added
to this JMenuItem with addMenuKeyListener().
MenuKeyListeners added or an empty
array if no listeners have been addedprivate void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException
s - the ObjectInputStream from which to read
IOException
ClassNotFoundExceptionReadObjectCallbackprivate void writeObject(ObjectOutputStream s) throws IOException
JComponentJComponent to an
ObjectOutputStream we temporarily uninstall its UI.
This is tricky to do because we want to uninstall
the UI before any of the JComponent's children
(or its LayoutManager etc.) are written,
and we don't want to restore the UI until the most derived
JComponent subclass has been been stored.
s - the ObjectOutputStream in which to write
IOExceptionprotected String paramString()
JMenuItem.
This method is intended to be used only for debugging purposes,
and the content and format of the returned string may vary between
implementations. The returned string may be empty but may not
be null.
paramString in class AbstractButtonJMenuItempublic AccessibleContext getAccessibleContext()
AccessibleContext associated with this
JMenuItem. For JMenuItems,
the AccessibleContext takes the form of an
AccessibleJMenuItem.
A new AccessibleJMenuItme instance is created if necessary.
getAccessibleContext in interface AccessiblegetAccessibleContext in class JComponentAccessibleJMenuItem that serves as the
AccessibleContext of this JMenuItem