JMenu (Java Platform SE 6)
javax.swing
Class JMenu
java.lang.Object java.awt.Component java.awt.Container javax.swing.JComponent javax.swing.AbstractButton javax.swing.JMenuItem javax.swing.JMenu
- All Implemented Interfaces:
- ImageObserver, ItemSelectable, MenuContainer, Serializable, Accessible, MenuElement, SwingConstants
public class JMenu
- extends JMenuItem
- implements Accessible, MenuElement
An implementation of a menu -- a popup window containing
JMenuItems that
is displayed when the user selects an item on the JMenuBar.
In addition to JMenuItems, a JMenu can
also contain JSeparators.
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
Actions. 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 JavaBeansTM
has been added to the java.beans package.
Please see XMLEncoder.
- See Also:
JMenuItem,JSeparator,JMenuBar,JPopupMenu
| Nested Class Summary | |
|---|---|
protected class |
JMenu.AccessibleJMenu
This class implements accessibility support for the JMenu class. |
protected class |
JMenu.WinListener
A listener class that watches for a popup window closing. |
| Nested classes/interfaces inherited from class javax.swing.JMenuItem |
|---|
JMenuItem.AccessibleJMenuItem |
| Nested classes/interfaces inherited from class javax.swing.AbstractButton |
|---|
AbstractButton.AccessibleAbstractButton, AbstractButton.ButtonChangeListener |
| Nested classes/interfaces inherited from class javax.swing.JComponent |
|---|
JComponent.AccessibleJComponent |
| Nested classes/interfaces inherited from class java.awt.Container |
|---|
Container.AccessibleAWTContainer |
| Nested classes/interfaces inherited from class java.awt.Component |
|---|
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy |
| Field Summary | |
|---|---|
protected JMenu.WinListener |
popupListener
The window-closing listener for the popup. |
| Fields inherited from class javax.swing.JComponent |
|---|
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW |
| Fields inherited from class java.awt.Component |
|---|
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
| Fields inherited from 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 |
| Fields inherited from interface java.awt.image.ImageObserver |
|---|
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
| Constructor Summary | |
|---|---|
JMenu()
Constructs a new JMenu with no text. |
|
JMenu(Action a)
Constructs a menu whose properties are taken from the Action supplied. |
|
JMenu(String s)
Constructs a new JMenu with the supplied string
as its text. |
|
JMenu(String s,
boolean b)
Constructs a new JMenu with the supplied string as
its text and specified as a tear-off menu or not. |
|
| Method Summary | |
|---|---|
JMenuItem |
add(Action a)
Creates a new menu item attached to the specified Action object and appends it to the end of this menu. |
Component |
add(Component c)
Appends a component to the end of this menu. |
Component |
add(Component c,
int index)
Adds the specified component to this container at the given position. |
JMenuItem |
add(JMenuItem menuItem)
Appends a menu item to the end of this menu. |
JMenuItem |
add(String s)
Creates a new menu item with the specified text and appends it to the end of this menu. |
void |
addMenuListener(MenuListener l)
Adds a listener for menu events. |
void |
addSeparator()
Appends a new separator to the end of the menu. |
void |
applyComponentOrientation(ComponentOrientation o)
Sets the ComponentOrientation property of this menu
and all components contained within it. |
protected PropertyChangeListener |
createActionChangeListener(JMenuItem b)
Returns a properly configured PropertyChangeListener
which updates the control as changes to the Action occur. |
protected JMenuItem |
createActionComponent(Action a)
Factory method which creates the JMenuItem for
Actions added to the JMenu. |
protected JMenu.WinListener |
createWinListener(JPopupMenu p)
Creates a window-closing listener for the popup. |
void |
doClick(int pressTime)
Programmatically performs a "click". |
protected void |
fireMenuCanceled()
Notifies all listeners that have registered interest for notification on this event type. |
protected void |
fireMenuDeselected()
Notifies all listeners that have registered interest for notification on this event type. |
protected void |
fireMenuSelected()
Notifies all listeners that have registered interest for notification on this event type. |
AccessibleContext |
getAccessibleContext()
Gets the AccessibleContext associated with this JMenu. |
Component |
getComponent()
Returns the java.awt.Component used to
paint this MenuElement. |
int |
getDelay()
Returns the suggested delay, in milliseconds, before submenus are popped up or down. |
JMenuItem |
getItem(int pos)
Returns the JMenuItem at the specified position. |
int |
getItemCount()
Returns the number of items on the menu, including separators. |
Component |
getMenuComponent(int n)
Returns the component at position n. |
int |
getMenuComponentCount()
Returns the number of components on the menu. |
Component[] |
getMenuComponents()
Returns an array of Components of the menu's
subcomponents. |
MenuListener[] |
getMenuListeners()
Returns an array of all the MenuListeners added
to this JMenu with addMenuListener(). |
JPopupMenu |
getPopupMenu()
Returns the popupmenu associated with this menu. |
protected Point |
getPopupMenuOrigin()
Computes the origin for the JMenu's popup menu. |
MenuElement[] |
getSubElements()
Returns an array of MenuElements containing the submenu
for this menu component. |
String |
getUIClassID()
Returns the name of the L&F class that renders this component. |
JMenuItem |
insert(Action a,
int pos)
Inserts a new menu item attached to the specified Action
object at a given position. |
JMenuItem |
insert(JMenuItem mi,
int pos)
Inserts the specified JMenuitem at a given position. |
void |
insert(String s,
int pos)
Inserts a new menu item with the specified text at a given position. |
void |
insertSeparator(int index)
Inserts a separator at the specified position. |
boolean |
isMenuComponent(Component c)
Returns true if the specified component exists in the submenu hierarchy. |
boolean |
isPopupMenuVisible()
Returns true if the menu's popup window is visible. |
boolean |
isSelected()
Returns true if the menu is currently selected (highlighted). |
boolean |
isTearOff()
Returns true if the menu can be torn off. |
boolean |
isTopLevelMenu()
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 |
paramString()
Returns a string representation of this JMenu. |
protected void |
processKeyEvent(KeyEvent evt)
Processes key stroke events such as mnemonics and accelerators. |
void |
remove(Component c)
Removes the component c from this menu. |
void |
remove(int pos)
Removes the menu item at the specified index from this menu. |
void |
remove(JMenuItem item)
Removes the specified menu item from this menu. |
void |
removeAll()
Removes all menu items from this menu. |
void |
removeMenuListener(MenuListener l)
Removes a listener for menu events. |
void |
setAccelerator(KeyStroke keyStroke)
setAccelerator is not defined for JMenu. |
void |
setComponentOrientation(ComponentOrientation o)
Sets the language-sensitive orientation that is to be used to order the elements or text within this component. |
void |
setDelay(int d)
Sets the suggested delay before the menu's PopupMenu
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 inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface javax.swing.MenuElement |
|---|
processKeyEvent, processMouseEvent |
| Field Detail |
|---|
popupListener
protected JMenu.WinListener popupListener
- The window-closing listener for the popup.
- See Also:
JMenu.WinListener
| Constructor Detail |
|---|
JMenu
public JMenu()
- Constructs a new
JMenuwith no text.
JMenu
public JMenu(String s)
- Constructs a new
JMenuwith the supplied string as its text.- Parameters:
s- the text for the menu label
JMenu
public JMenu(Action a)
- Constructs a menu whose properties are taken from the
Actionsupplied.- Parameters:
a- anAction- Since:
- 1.3
JMenu
public JMenu(String s, boolean b)
- Constructs a new
JMenuwith the supplied string as its text and specified as a tear-off menu or not.- Parameters:
s- the text for the menu labelb- can the menu be torn off (not yet implemented)
| Method Detail |
|---|
updateUI
public void updateUI()
- Resets the UI property with a value from the current look and feel.
- See Also:
JComponent.updateUI()
getUIClassID
public String getUIClassID()
- Returns the name of the L&F class that renders this component.
- Overrides:
getUIClassIDin classJMenuItem
- Returns:
- the string "MenuUI"
- See Also:
JComponent.getUIClassID(),UIDefaults.getUI(javax.swing.JComponent)
setModel
public void setModel(ButtonModel newModel)
- Sets the data model for the "menu button" -- the label
that the user clicks to open or close the menu.
- Parameters:
newModel- theButtonModel- See Also:
AbstractButton.getModel()
isSelected
public boolean isSelected()
- Returns true if the menu is currently selected (highlighted).
- Overrides:
isSelectedin classAbstractButton
- Returns:
- true if the menu is selected, else false
setSelected
public void setSelected(boolean b)
- Sets the selection status of the menu.
- Overrides:
setSelectedin classAbstractButton
- Parameters:
b- true to select (highlight) the menu; false to de-select the menu
isPopupMenuVisible
public boolean isPopupMenuVisible()
- Returns true if the menu's popup window is visible.
- Returns:
- true if the menu is visible, else false
setPopupMenuVisible
public void setPopupMenuVisible(boolean b)
- Sets the visibility of the menu's popup. If the menu is
not enabled, this method will have no effect.
- Parameters:
b- a boolean value -- true to make the menu visible, false to hide it
getPopupMenuOrigin
protected Point getPopupMenuOrigin()
- Computes the origin for the
JMenu's popup menu. This method uses Look and Feel properties namedMenu.menuPopupOffsetX,Menu.menuPopupOffsetY,Menu.submenuPopupOffsetX, andMenu.submenuPopupOffsetYto adjust the exact location of popup. - Returns:
- a
Pointin the coordinate space of the menu which should be used as the origin of theJMenu's popup menu - Since:
- 1.3
getDelay
public int getDelay()
- Returns the suggested delay, in milliseconds, before submenus
are popped up or down.
Each look and feel (L&F) may determine its own policy for
observing the
delayproperty. In most cases, the delay is not observed for top level menus or while dragging. The default fordelayis 0. This method is a property of the look and feel code and is used to manage the idiosyncracies of the various UI implementations. - Returns:
- the
delayproperty
setDelay
public void setDelay(int d)
- Sets the suggested delay before the menu's
PopupMenuis popped up or down. Each look and feel (L&F) may determine it's own policy for observing the delay property. In most cases, the delay is not observed for top level menus or while dragging. This method is a property of the look and feel code and is used to manage the idiosyncracies of the various UI implementations. - Parameters:
d- the number of milliseconds to delay- Throws:
IllegalArgumentException- ifdis less than 0
setMenuLocation
public void setMenuLocation(int x,
int y)
- Sets the location of the popup component.
- Parameters:
x- the x coordinate of the popup's new positiony- the y coordinate of the popup's new position
add
public JMenuItem add(JMenuItem menuItem)
- Appends a menu item to the end of this menu.
Returns the menu item added.
- Parameters:
menuItem- theJMenuitemto be added- Returns:
- the
JMenuItemadded
add
public Component add(Component c)
- Appends a component to the end of this menu.
Returns the component added.
- Parameters:
c- theComponentto add- Returns:
- the
Componentadded - See Also:
Container.addImpl(java.awt.Component, java.lang.Object, int),Container.validate(),JComponent.revalidate()
add
public Component add(Component c, int index)
- Adds the specified component to this container at the given
position. If
indexequals -1, the component will be appended to the end. - Parameters:
c- theComponentto addindex- the position at which to insert the component- Returns:
- the
Componentadded - See Also:
remove(javax.swing.JMenuItem),Container.add(Component, int)
add
public JMenuItem add(String s)
- Creates a new menu item with the specified text and appends
it to the end of this menu.
- Parameters:
s- the string for the menu item to be added
add
public JMenuItem add(Action a)
- Creates a new menu item attached to the specified
Actionobject and appends it to the end of this menu. - Parameters:
a- theActionfor the menu item to be added- See Also:
Action
createActionComponent
protected JMenuItem createActionComponent(Action a)
- Factory method which creates the
JMenuItemforActions added to theJMenu. - Parameters:
a- theActionfor the menu item to be added- Returns:
- the new menu item
- Since:
- 1.3
- See Also:
Action
createActionChangeListener
protected PropertyChangeListener createActionChangeListener(JMenuItem b)
- Returns a properly configured
PropertyChangeListenerwhich updates the control as changes to theActionoccur.
addSeparator
public void addSeparator()
- Appends a new separator to the end of the menu.
insert
public void insert(String s, int pos)
- Inserts a new menu item with the specified text at a
given position.
- Parameters:
s- the text for the menu item to addpos- an integer specifying the position at which to add the new menu item- Throws:
IllegalArgumentException- when the value ofpos< 0
insert
public JMenuItem insert(JMenuItem mi, int pos)
- Inserts the specified
JMenuitemat a given position. - Parameters:
mi- theJMenuitemto addpos- an integer specifying the position at which to add the newJMenuitem- Returns:
- the new menu item
- Throws:
IllegalArgumentException- if the value ofpos< 0
insert
public JMenuItem insert(Action a, int pos)
- Inserts a new menu item attached to the specified
Actionobject at a given position. - Parameters:
a- theActionobject for the menu item to addpos- an integer specifying the position at which to add the new menu item- Throws:
IllegalArgumentException- if the value ofpos< 0
insertSeparator
public void insertSeparator(int index)
- Inserts a separator at the specified position.
- Parameters:
index- an integer specifying the position at which to insert the menu separator- Throws:
IllegalArgumentException- if the value ofindex< 0
getItem
public JMenuItem getItem(int pos)
- Returns the
JMenuItemat the specified position. If the component atposis not a menu item,nullis returned. This method is included for AWT compatibility. - Parameters:
pos- an integer specifying the position- Returns:
- the menu item at the specified position; or
nullif the item as the specified position is not a menu item - Throws:
IllegalArgumentException- if the value ofpos< 0
getItemCount
public int getItemCount()
- Returns the number of items on the menu, including separators.
This method is included for AWT compatibility.
- Returns:
- an integer equal to the number of items on the menu
- See Also:
getMenuComponentCount()
isTearOff
public boolean isTearOff()
- Returns true if the menu can be torn off. This method is not
yet implemented.
- Returns:
- true if the menu can be torn off, else false
- Throws:
Error- if invoked -- this method is not yet implemented
remove
public void remove(JMenuItem item)
- Removes the specified menu item from this menu. If there is no
popup menu, this method will have no effect.
- Parameters:
item- theJMenuItemto be removed from the menu
remove
public void remove(int pos)
- Removes the menu item at the specified index from this menu.
- Parameters:
pos- the position of the item to be removed- Throws:
IllegalArgumentException- if the value ofpos< 0, or ifposis greater than the number of menu items- See Also:
Container.add(java.awt.Component),Container.validate(),Container.getComponentCount()
remove
public void remove(Component c)
- Removes the component
cfrom this menu. - Parameters:
c- the component to be removed- See Also:
Container.add(java.awt.Component),Container.validate(),Container.remove(int)
removeAll
public void removeAll()
- Removes all menu items from this menu.
getMenuComponentCount
public int getMenuComponentCount()
- Returns the number of components on the menu.
- Returns:
- an integer containing the number of components on the menu
getMenuComponent
public Component getMenuComponent(int n)
- Returns the component at position
n. - Parameters:
n- the position of the component to be returned- Returns:
- the component requested, or
nullif there is no popup menu
getMenuComponents
public Component[] getMenuComponents()
- Returns an array of
Components of the menu's subcomponents. Note that this returns allComponents in the popup menu, including separators. - Returns:
- an array of
Components or an empty array if there is no popup menu
isTopLevelMenu
public boolean isTopLevelMenu()
- Returns true if the menu is a 'top-level menu', that is, if it is
the direct child of a menubar.
- Returns:
- true if the menu is activated from the menu bar; false if the menu is activated from a menu item on another menu
isMenuComponent
public boolean isMenuComponent(Component c)
- Returns true if the specified component exists in the
submenu hierarchy.
- Parameters:
c- theComponentto be tested- Returns:
- true if the
Componentexists, false otherwise
getPopupMenu
public JPopupMenu getPopupMenu()
- Returns the popupmenu associated with this menu. If there is no popupmenu, it will create one.
addMenuListener
public void addMenuListener(MenuListener l)
- Adds a listener for menu events.
- Parameters:
l- the listener to be added
removeMenuListener
public void removeMenuListener(MenuListener l)
- Removes a listener for menu events.
- Parameters:
l- the listener to be removed
getMenuListeners
public MenuListener[] getMenuListeners()
- Returns an array of all the
MenuListeners added to this JMenu with addMenuListener(). - Returns:
- all of the
MenuListeners added or an empty array if no listeners have been added - Since:
- 1.4
fireMenuSelected
protected void fireMenuSelected()
- Notifies all listeners that have registered interest for
notification on this event type. The event instance
is created lazily.
- Throws:
Error- if there is anulllistener- See Also:
EventListenerList
fireMenuDeselected
protected void fireMenuDeselected()
- Notifies all listeners that have registered interest for
notification on this event type. The event instance
is created lazily.
- Throws:
Error- if there is anulllistener- See Also:
EventListenerList
fireMenuCanceled
protected void fireMenuCanceled()
- Notifies all listeners that have registered interest for
notification on this event type. The event instance
is created lazily.
- Throws:
Error- if there is anulllistener- See Also:
EventListenerList
createWinListener
protected JMenu.WinListener createWinListener(JPopupMenu p)
- Creates a window-closing listener for the popup.
- Parameters:
p- theJPopupMenu- Returns:
- the new window-closing listener
- See Also:
JMenu.WinListener
menuSelectionChanged
public void menuSelectionChanged(boolean isIncluded)
- Messaged when the menubar selection changes to activate or
deactivate this menu.
Overrides
JMenuItem.menuSelectionChanged. - Specified by:
menuSelectionChangedin interfaceMenuElement- Overrides:
menuSelectionChangedin classJMenuItem
- Parameters:
isIncluded- true if this menu is active, false if it is not- See Also:
MenuSelectionManager.setSelectedPath(MenuElement[])
getSubElements
public MenuElement[] getSubElements()
- Returns an array of
MenuElements containing the submenu for this menu component. If popup menu isnullreturns an empty array. This method is required to conform to theMenuElementinterface. Note that sinceJSeparators do not conform to theMenuElementinterface, this array will only containJMenuItems. - Specified by:
getSubElementsin interfaceMenuElement- Overrides:
getSubElementsin classJMenuItem
- Returns:
- an array of
MenuElementobjects
getComponent
public Component getComponent()
- Returns the
java.awt.Componentused to paint thisMenuElement. The returned component is used to convert events and detect if an event is inside a menu component. - Specified by:
getComponentin interfaceMenuElement- Overrides:
getComponentin classJMenuItem
- Returns:
- the
Componentthat paints this menu item
applyComponentOrientation
public void applyComponentOrientation(ComponentOrientation o)
- Sets the
ComponentOrientationproperty of this menu and all components contained within it. This includes all components returned bygetMenuComponents. - Overrides:
applyComponentOrientationin classContainer
- Parameters:
o- the new component orientation of this menu and the components contained within it.- Throws:
NullPointerException- iforientationis null.- Since:
- 1.4
- See Also:
Component.setComponentOrientation(java.awt.ComponentOrientation),Component.getComponentOrientation()
setComponentOrientation
public void setComponentOrientation(ComponentOrientation o)
- Description copied from class:
Component - Sets the language-sensitive orientation that is to be used to order
the elements or text within this component. Language-sensitive
LayoutManagerandComponentsubclasses will use this property to determine how to lay out and draw components.At construction time, a component's orientation is set to
ComponentOrientation.UNKNOWN, indicating that it has not been specified explicitly. The UNKNOWN orientation behaves the same asComponentOrientation.LEFT_TO_RIGHT.To set the orientation of a single component, use this method. To set the orientation of an entire component hierarchy, use
applyComponentOrientation. - Overrides:
setComponentOrientationin classComponent
- See Also:
ComponentOrientation
setAccelerator
public void setAccelerator(KeyStroke keyStroke)
setAcceleratoris not defined forJMenu. UsesetMnemonicinstead.- Overrides:
setAcceleratorin classJMenuItem
- Parameters:
keyStroke- the keystroke combination which will invoke theJMenuItem's actionlisteners without navigating the menu hierarchy- Throws:
Error- if invoked -- this method is not defined for JMenu. UsesetMnemonicinstead
processKeyEvent
protected void processKeyEvent(KeyEvent evt)
- Processes key stroke events such as mnemonics and accelerators.
- Overrides:
processKeyEventin classJComponent
- Parameters:
evt- the key event to be processed- See Also:
KeyEvent,KeyListener,KeyboardFocusManager,DefaultKeyboardFocusManager,Component.processEvent(java.awt.AWTEvent),Component.dispatchEvent(java.awt.AWTEvent),Component.addKeyListener(java.awt.event.KeyListener),Component.enableEvents(long),Component.isShowing()
doClick
public void doClick(int pressTime)
- Programmatically performs a "click". This overrides the method
AbstractButton.doClickin order to make the menu pop up. - Overrides:
doClickin classAbstractButton
- Parameters:
pressTime- indicates the number of milliseconds the button was pressed for
paramString
protected String paramString()
- Returns a string representation of this
JMenu. 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 benull. - Overrides:
paramStringin classJMenuItem
- Returns:
- a string representation of this JMenu.
getAccessibleContext
public AccessibleContext getAccessibleContext()
- Gets the AccessibleContext associated with this JMenu.
For JMenus, the AccessibleContext takes the form of an
AccessibleJMenu.
A new AccessibleJMenu instance is created if necessary.
- Specified by:
getAccessibleContextin interfaceAccessible- Overrides:
getAccessibleContextin classJMenuItem
- Returns:
- an AccessibleJMenu that serves as the AccessibleContext of this JMenu
Submit a bug or feature
For further API reference and developer documentation, see Java SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2015, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.