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 JavaBeans™ has been added to the java.beans package. Please see XMLEncoder.
JMenuItem, JSeparator, JMenuBar, JPopupMenu
| Modifier and Type | Class and Description |
|---|---|
protected class |
JMenu.AccessibleJMenu This class implements accessibility support for the |
protected class |
JMenu.WinListener A listener class that watches for a popup window closing. |
JMenuItem.AccessibleJMenuItem AbstractButton.AccessibleAbstractButton, AbstractButton.ButtonChangeListener JComponent.AccessibleJComponent Container.AccessibleAWTContainer Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy protected JMenu.WinListener popupListener
The window-closing listener for the popup.
JMenu.WinListenerpublic JMenu()
Constructs a new JMenu with no text.
public JMenu(String s)
Constructs a new JMenu with the supplied string as its text.
s - the text for the menu labelpublic JMenu(Action a)
Constructs a menu whose properties are taken from the Action supplied.
a - an Action
public 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.
s - the text for the menu labelb - can the menu be torn off (not yet implemented)public void updateUI()
Resets the UI property with a value from the current look and feel.
updateUI in class JMenuItem
JComponent.updateUI()public String getUIClassID()
Returns the name of the L&F class that renders this component.
getUIClassID in class JMenuItem
JComponent.getUIClassID(), UIDefaults.getUI(javax.swing.JComponent)
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.
setModel in class JMenuItem
newModel - the ButtonModel
AbstractButton.getModel()public boolean isSelected()
Returns true if the menu is currently selected (highlighted).
isSelected in class AbstractButton
public void setSelected(boolean b)
Sets the selection status of the menu.
setSelected in class AbstractButton
b - true to select (highlight) the menu; false to de-select the menupublic boolean isPopupMenuVisible()
Returns true if the menu's popup window is visible.
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.
b - a boolean value -- true to make the menu visible, false to hide itprotected Point getPopupMenuOrigin()
Computes the origin for the JMenu's popup menu. This method uses Look and Feel properties named Menu.menuPopupOffsetX, Menu.menuPopupOffsetY, Menu.submenuPopupOffsetX, and Menu.submenuPopupOffsetY to adjust the exact location of popup.
Point in the coordinate space of the menu which should be used as the origin of the JMenu's popup menupublic 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 delay property. In most cases, the delay is not observed for top level menus or while dragging. The default for delay is 0. This method is a property of the look and feel code and is used to manage the idiosyncrasies of the various UI implementations.
delay propertypublic void setDelay(int d)
Sets the suggested delay before the menu's PopupMenu is 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 idiosyncrasies of the various UI implementations.
d - the number of milliseconds to delayIllegalArgumentException - if d is less than 0public void setMenuLocation(int x,
int y) Sets the location of the popup component.
x - the x coordinate of the popup's new positiony - the y coordinate of the popup's new positionpublic JMenuItem add(JMenuItem menuItem)
Appends a menu item to the end of this menu. Returns the menu item added.
menuItem - the JMenuitem to be addedJMenuItem addedpublic Component add(Component c)
Appends a component to the end of this menu. Returns the component added.
add in class Container
c - the Component to addComponent addedContainer.addImpl(java.awt.Component, java.lang.Object, int), Container.invalidate(), Container.validate(), JComponent.revalidate()
public Component add(Component c,
int index) Adds the specified component to this container at the given position. If index equals -1, the component will be appended to the end.
add in class Container
c - the Component to addindex - the position at which to insert the componentComponent addedremove(javax.swing.JMenuItem), Container.add(Component, int)
public JMenuItem add(String s)
Creates a new menu item with the specified text and appends it to the end of this menu.
s - the string for the menu item to be addedpublic JMenuItem add(Action a)
Creates a new menu item attached to the specified Action object and appends it to the end of this menu.
a - the Action for the menu item to be addedActionprotected JMenuItem createActionComponent(Action a)
Factory method which creates the JMenuItem for Actions added to the JMenu.
a - the Action for the menu item to be addedActionprotected PropertyChangeListener createActionChangeListener(JMenuItem b)
Returns a properly configured PropertyChangeListener which updates the control as changes to the Action occur.
public void addSeparator()
Appends a new separator to the end of the menu.
public void insert(String s,
int pos) Inserts a new menu item with the specified text at a given position.
s - the text for the menu item to addpos - an integer specifying the position at which to add the new menu itemIllegalArgumentException - when the value of pos < 0public JMenuItem insert(JMenuItem mi,
int pos) Inserts the specified JMenuitem at a given position.
mi - the JMenuitem to addpos - an integer specifying the position at which to add the new JMenuitem
IllegalArgumentException - if the value of pos < 0public JMenuItem insert(Action a,
int pos) Inserts a new menu item attached to the specified Action object at a given position.
a - the Action object for the menu item to addpos - an integer specifying the position at which to add the new menu itemIllegalArgumentException - if the value of pos < 0public void insertSeparator(int index)
Inserts a separator at the specified position.
index - an integer specifying the position at which to insert the menu separatorIllegalArgumentException - if the value of index < 0public JMenuItem getItem(int pos)
Returns the JMenuItem at the specified position. If the component at pos is not a menu item, null is returned. This method is included for AWT compatibility.
pos - an integer specifying the positionnull if the item as the specified position is not a menu itemIllegalArgumentException - if the value of pos < 0public int getItemCount()
Returns the number of items on the menu, including separators. This method is included for AWT compatibility.
getMenuComponentCount()public boolean isTearOff()
Returns true if the menu can be torn off. This method is not yet implemented.
Error - if invoked -- this method is not yet implementedpublic void remove(JMenuItem item)
Removes the specified menu item from this menu. If there is no popup menu, this method will have no effect.
item - the JMenuItem to be removed from the menupublic void remove(int pos)
Removes the menu item at the specified index from this menu.
remove in class Container
pos - the position of the item to be removedIllegalArgumentException - if the value of pos < 0, or if pos is greater than the number of menu itemsContainer.add(java.awt.Component), Container.invalidate(), Container.validate(), Container.getComponentCount()
public void remove(Component c)
Removes the component c from this menu.
remove in class Container
c - the component to be removedContainer.add(java.awt.Component), Container.invalidate(), Container.validate(), Container.remove(int)
public void removeAll()
Removes all menu items from this menu.
removeAll in class Container
Container.add(java.awt.Component), Container.remove(int), Container.invalidate()
public int getMenuComponentCount()
Returns the number of components on the menu.
public Component getMenuComponent(int n)
Returns the component at position n.
n - the position of the component to be returnednull if there is no popup menupublic Component[] getMenuComponents()
Returns an array of Components of the menu's subcomponents. Note that this returns all Components in the popup menu, including separators.
Components or an empty array if there is no popup menupublic boolean isTopLevelMenu()
Returns true if the menu is a 'top-level menu', that is, if it is the direct child of a menubar.
public boolean isMenuComponent(Component c)
Returns true if the specified component exists in the submenu hierarchy.
c - the Component to be testedComponent exists, false otherwisepublic JPopupMenu getPopupMenu()
Returns the popupmenu associated with this menu. If there is no popupmenu, it will create one.
public void addMenuListener(MenuListener l)
Adds a listener for menu events.
l - the listener to be addedpublic void removeMenuListener(MenuListener l)
Removes a listener for menu events.
l - the listener to be removedpublic MenuListener[] getMenuListeners()
Returns an array of all the MenuListeners added to this JMenu with addMenuListener().
MenuListeners added or an empty array if no listeners have been addedprotected void fireMenuSelected()
Notifies all listeners that have registered interest for notification on this event type. The event instance is created lazily.
Error - if there is a null listenerEventListenerListprotected void fireMenuDeselected()
Notifies all listeners that have registered interest for notification on this event type. The event instance is created lazily.
Error - if there is a null listenerEventListenerListprotected void fireMenuCanceled()
Notifies all listeners that have registered interest for notification on this event type. The event instance is created lazily.
Error - if there is a null listenerEventListenerListprotected JMenu.WinListener createWinListener(JPopupMenu p)
Creates a window-closing listener for the popup.
p - the JPopupMenu
JMenu.WinListenerpublic void menuSelectionChanged(boolean isIncluded)
Messaged when the menubar selection changes to activate or deactivate this menu. Overrides JMenuItem.menuSelectionChanged.
menuSelectionChanged in interface MenuElement
menuSelectionChanged in class JMenuItem
isIncluded - true if this menu is active, false if it is notMenuSelectionManager.setSelectedPath(MenuElement[])public MenuElement[] getSubElements()
Returns an array of MenuElements containing the submenu for this menu component. If popup menu is null returns an empty array. This method is required to conform to the MenuElement interface. Note that since JSeparators do not conform to the MenuElement interface, this array will only contain JMenuItems.
getSubElements in interface MenuElement
getSubElements in class JMenuItem
MenuElement objectspublic Component getComponent()
Returns the java.awt.Component used to paint this MenuElement. The returned component is used to convert events and detect if an event is inside a menu component.
getComponent in interface MenuElement
getComponent in class JMenuItem
Component that paints this menu itempublic void applyComponentOrientation(ComponentOrientation o)
Sets the ComponentOrientation property of this menu and all components contained within it. This includes all components returned by getMenuComponents.
applyComponentOrientation in class Container
o - the new component orientation of this menu and the components contained within it.NullPointerException - if orientation is null.Component.setComponentOrientation(java.awt.ComponentOrientation), Component.getComponentOrientation()
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 LayoutManager and Component subclasses 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 as ComponentOrientation.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.
This method changes layout-related information, and therefore, invalidates the component hierarchy.
setComponentOrientation in class Component
ComponentOrientation, Component.invalidate()
public void setAccelerator(KeyStroke keyStroke)
setAccelerator is not defined for JMenu. Use setMnemonic instead.
setAccelerator in class JMenuItem
keyStroke - the keystroke combination which will invoke the JMenuItem's actionlisteners without navigating the menu hierarchyError - if invoked -- this method is not defined for JMenu. Use setMnemonic insteadprotected void processKeyEvent(KeyEvent evt)
Processes key stroke events such as mnemonics and accelerators.
processKeyEvent in class JComponent
evt - the key event to be processedKeyEvent, KeyListener, KeyboardFocusManager, DefaultKeyboardFocusManager, Component.processEvent(java.awt.AWTEvent), Component.dispatchEvent(java.awt.AWTEvent), Component.addKeyListener(java.awt.event.KeyListener), Component.enableEvents(long), Component.isShowing()
public void doClick(int pressTime)
Programmatically performs a "click". This overrides the method AbstractButton.doClick in order to make the menu pop up.
doClick in class AbstractButton
pressTime - indicates the number of milliseconds the button was pressed forprotected 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 be null.
paramString in class JMenuItem
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.
getAccessibleContext in interface Accessible
getAccessibleContext in class JMenuItem
© 1993–2017, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.