- All Implemented Interfaces:
ImageObserver
,ItemSelectable
,MenuContainer
,Serializable
,Accessible
,MenuElement
,SwingConstants
JMenuItem
s that
is displayed when the user selects an item on the JMenuBar
.
In addition to JMenuItem
s, a JMenu
can
also contain JSeparator
s.
In essence, a menu is a button with an associated JPopupMenu
.
When the "button" is pressed, the JPopupMenu
appears. If the
"button" is on the JMenuBar
, the menu is a top-level window.
If the "button" is another menu item, then the JPopupMenu
is
"pull-right" menu.
Menus can be configured, and to some degree controlled, by
Action
s. Using an
Action
with a menu has many benefits beyond directly
configuring a menu. 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 information and examples of using menus see How to Use Menus, a section 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:
JMenuItem
,JSeparator
,JMenuBar
,JPopupMenu
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected class
This class implements accessibility support for theJMenu
class.protected class
A listener class that watches for a popup window closing.Nested classes/interfaces declared in class javax.swing.JMenuItem
JMenuItem.AccessibleJMenuItem
Nested classes/interfaces declared in class javax.swing.AbstractButton
AbstractButton.AccessibleAbstractButton, AbstractButton.ButtonChangeListener
Nested classes/interfaces declared in class javax.swing.JComponent
JComponent.AccessibleJComponent
Nested classes/interfaces declared in class java.awt.Container
Container.AccessibleAWTContainer
Nested classes/interfaces declared in class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected JMenu.WinListener
The window-closing listener for the popup.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
ConstructorsConstructorDescriptionJMenu()
Constructs a newJMenu
with no text.Constructs a newJMenu
with the supplied string as its text.Constructs a newJMenu
with the supplied string as its text and specified as a tear-off menu or not.Constructs a menu whose properties are taken from theAction
supplied. -
Method Summary
Modifier and TypeMethodDescriptionAppends a component to the end of this menu.Adds the specified component to this container at the given position.Creates a new menu item with the specified text and appends it to the end of this menu.Creates a new menu item attached to the specifiedAction
object and appends it to the end of this menu.Appends a menu item to the end of this menu.void
Adds a listener for menu events.void
Appends a new separator to the end of the menu.void
Sets theComponentOrientation
property of this menu and all components contained within it.protected PropertyChangeListener
Returns a properly configuredPropertyChangeListener
which updates the control as changes to theAction
occur.protected JMenuItem
Factory method which creates theJMenuItem
forAction
s added to theJMenu
.protected JMenu.WinListener
Creates a window-closing listener for the popup.void
doClick(int pressTime)
Programmatically performs a "click".protected void
Notifies all listeners that have registered interest for notification on this event type.protected void
Notifies all listeners that have registered interest for notification on this event type.protected void
Notifies all listeners that have registered interest for notification on this event type.Gets the AccessibleContext associated with this JMenu.Returns thejava.awt.Component
used to paint thisMenuElement
.int
getDelay()
Returns the suggested delay, in milliseconds, before submenus are popped up or down.getItem(int pos)
Returns theJMenuItem
at the specified position.int
Returns the number of items on the menu, including separators.getMenuComponent(int n)
Returns the component at positionn
.int
Returns the number of components on the menu.Returns an array ofComponent
s of the menu's subcomponents.Returns an array of all theMenuListener
s added to this JMenu with addMenuListener().Returns the popupmenu associated with this menu.protected Point
Computes the origin for theJMenu
's popup menu.Returns an array ofMenuElement
s containing the submenu for this menu component.Returns the name of the L&F class that renders this component.void
Inserts a new menu item with the specified text at a given position.Inserts a new menu item attached to the specifiedAction
object at a given position.Inserts the specifiedJMenuitem
at a given position.void
insertSeparator(int index)
Inserts a separator at the specified position.boolean
Returns true if the specified component exists in the submenu hierarchy.boolean
Returns true if the menu's popup window is visible.boolean
Returns true if the menu is currently selected (highlighted).boolean
Returns true if the menu can be torn off.boolean
Returns true if the menu is a 'top-level menu', that is, if it is the direct child of a menubar.void
menuSelectionChanged(boolean isIncluded)
Messaged when the menubar selection changes to activate or deactivate this menu.protected String
Returns a string representation of thisJMenu
.protected void
processKeyEvent(KeyEvent evt)
Processes key stroke events such as mnemonics and accelerators.void
remove(int pos)
Removes the menu item at the specified index from this menu.void
Removes the componentc
from this menu.void
Removes the specified menu item from this menu.void
Removes all menu items from this menu.void
Removes a listener for menu events.void
setAccelerator(KeyStroke keyStroke)
setAccelerator
is not defined forJMenu
.void
setDelay(int d)
Sets the suggested delay before the menu'sPopupMenu
is popped up or down.void
setMenuLocation(int x, int y)
Sets the location of the popup component.void
setModel(ButtonModel newModel)
Sets the data model for the "menu button" -- the label that the user clicks to open or close the menu.void
setPopupMenuVisible(boolean b)
Sets the visibility of the menu's popup.void
setSelected(boolean b)
Sets the selection status of the menu.void
updateUI()
Resets the UI property with a value from the current look and feel.Methods declared in class javax.swing.JMenuItem
actionPropertyChanged, addMenuDragMouseListener, addMenuKeyListener, configurePropertiesFromAction, fireMenuDragMouseDragged, fireMenuDragMouseEntered, fireMenuDragMouseExited, fireMenuDragMouseReleased, fireMenuKeyPressed, fireMenuKeyReleased, fireMenuKeyTyped, getAccelerator, getMenuDragMouseListeners, getMenuKeyListeners, init, isArmed, processKeyEvent, processMenuDragMouseEvent, processMenuKeyEvent, processMouseEvent, removeMenuDragMouseListener, removeMenuKeyListener, setArmed, setEnabled, setUI
Methods declared in class javax.swing.AbstractButton
addActionListener, addChangeListener, addImpl, addItemListener, checkHorizontalKey, checkVerticalKey, createActionListener, createActionPropertyChangeListener, createChangeListener, createItemListener, doClick, fireActionPerformed, fireItemStateChanged, fireStateChanged, getAction, getActionCommand, getActionListeners, getChangeListeners, getDisabledIcon, getDisabledSelectedIcon, getDisplayedMnemonicIndex, getHideActionText, getHorizontalAlignment, getHorizontalTextPosition, getIcon, getIconTextGap, getItemListeners, getLabel, getMargin, getMnemonic, getModel, getMultiClickThreshhold, getPressedIcon, getRolloverIcon, getRolloverSelectedIcon, getSelectedIcon, getSelectedObjects, getText, getUI, getVerticalAlignment, getVerticalTextPosition, imageUpdate, isBorderPainted, isContentAreaFilled, isFocusPainted, isRolloverEnabled, paintBorder, removeActionListener, removeChangeListener, removeItemListener, removeNotify, setAction, setActionCommand, setBorderPainted, setContentAreaFilled, setDisabledIcon, setDisabledSelectedIcon, setDisplayedMnemonicIndex, setFocusPainted, setHideActionText, setHorizontalAlignment, setHorizontalTextPosition, setIcon, setIconTextGap, setLabel, setLayout, setMargin, setMnemonic, setMnemonic, setMultiClickThreshhold, setPressedIcon, setRolloverEnabled, setRolloverIcon, setRolloverSelectedIcon, setSelectedIcon, setText, setUI, setVerticalAlignment, setVerticalTextPosition
Methods declared in class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale,