Menu (Java Platform SE 8 ) (original) (raw)
- java.awt.MenuComponent
- java.awt.MenuItem
- java.awt.Menu
- java.awt.MenuItem
All Implemented Interfaces:
MenuContainer, Serializable, Accessible
Direct Known Subclasses:
PopupMenu
public class Menu
extends MenuItem
implements MenuContainer, Accessible
A Menu
object is a pull-down menu component that is deployed from a menu bar.
A menu can optionally be a tear-off menu. A tear-off menu can be opened and dragged away from its parent menu bar or menu. It remains on the screen after the mouse button has been released. The mechanism for tearing off a menu is platform dependent, since the look and feel of the tear-off menu is determined by its peer. On platforms that do not support tear-off menus, the tear-off property is ignored.
Each item in a menu must belong to the MenuItem
class. It can be an instance of MenuItem
, a submenu (an instance of Menu
), or a check box (an instance ofCheckboxMenuItem
).
Since:
JDK1.0
See Also:
MenuItem, CheckboxMenuItem, Serialized Form
Nested Class Summary
Nested Classes
Modifier and Type Class Description protected class Menu.AccessibleAWTMenu Inner class of Menu used to provide default support for accessibility. * ### Nested classes/interfaces inherited from class java.awt.[MenuItem](../../java/awt/MenuItem.html "class in java.awt") `[MenuItem.AccessibleAWTMenuItem](../../java/awt/MenuItem.AccessibleAWTMenuItem.html "class in java.awt")` * ### Nested classes/interfaces inherited from class java.awt.[MenuComponent](../../java/awt/MenuComponent.html "class in java.awt") `[MenuComponent.AccessibleAWTMenuComponent](../../java/awt/MenuComponent.AccessibleAWTMenuComponent.html "class in java.awt")`
Constructor Summary
Constructors
Constructor Description Menu() Constructs a new menu with an empty label. Menu(String label) Constructs a new menu with the specified label. Menu(String label, boolean tearOff) Constructs a new menu with the specified label, indicating whether the menu can be torn off. Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods
Modifier and Type Method Description MenuItem add(MenuItem mi) Adds the specified menu item to this menu. void add(String label) Adds an item with the specified label to this menu. void addNotify() Creates the menu's peer. void addSeparator() Adds a separator line, or a hypen, to the menu at the current position. int countItems() Deprecated. AccessibleContext getAccessibleContext() Gets the AccessibleContext associated with this Menu. MenuItem getItem(int index) Gets the item located at the specified index of this menu. int getItemCount() Get the number of items in this menu. void insert(MenuItem menuitem, int index) Inserts a menu item into this menu at the specified position. void insert(String label, int index) Inserts a menu item with the specified label into this menu at the specified position. void insertSeparator(int index) Inserts a separator at the specified position. boolean isTearOff() Indicates whether this menu is a tear-off menu. String paramString() Returns a string representing the state of this Menu. void remove(int index) Removes the menu item at the specified index from this menu. void remove(MenuComponent item) Removes the specified menu item from this menu. void removeAll() Removes all items from this menu. void removeNotify() Removes the menu's peer. * ### Methods inherited from class java.awt.[MenuItem](../../java/awt/MenuItem.html "class in java.awt") `[addActionListener](../../java/awt/MenuItem.html#addActionListener-java.awt.event.ActionListener-), [deleteShortcut](../../java/awt/MenuItem.html#deleteShortcut--), [disable](../../java/awt/MenuItem.html#disable--), [disableEvents](../../java/awt/MenuItem.html#disableEvents-long-), [enable](../../java/awt/MenuItem.html#enable--), [enable](../../java/awt/MenuItem.html#enable-boolean-), [enableEvents](../../java/awt/MenuItem.html#enableEvents-long-), [getActionCommand](../../java/awt/MenuItem.html#getActionCommand--), [getActionListeners](../../java/awt/MenuItem.html#getActionListeners--), [getLabel](../../java/awt/MenuItem.html#getLabel--), [getListeners](../../java/awt/MenuItem.html#getListeners-java.lang.Class-), [getShortcut](../../java/awt/MenuItem.html#getShortcut--), [isEnabled](../../java/awt/MenuItem.html#isEnabled--), [processActionEvent](../../java/awt/MenuItem.html#processActionEvent-java.awt.event.ActionEvent-), [processEvent](../../java/awt/MenuItem.html#processEvent-java.awt.AWTEvent-), [removeActionListener](../../java/awt/MenuItem.html#removeActionListener-java.awt.event.ActionListener-), [setActionCommand](../../java/awt/MenuItem.html#setActionCommand-java.lang.String-), [setEnabled](../../java/awt/MenuItem.html#setEnabled-boolean-), [setLabel](../../java/awt/MenuItem.html#setLabel-java.lang.String-), [setShortcut](../../java/awt/MenuItem.html#setShortcut-java.awt.MenuShortcut-)` * ### Methods inherited from class java.awt.[MenuComponent](../../java/awt/MenuComponent.html "class in java.awt") `[dispatchEvent](../../java/awt/MenuComponent.html#dispatchEvent-java.awt.AWTEvent-), [getFont](../../java/awt/MenuComponent.html#getFont--), [getName](../../java/awt/MenuComponent.html#getName--), [getParent](../../java/awt/MenuComponent.html#getParent--), [getPeer](../../java/awt/MenuComponent.html#getPeer--), [getTreeLock](../../java/awt/MenuComponent.html#getTreeLock--), [postEvent](../../java/awt/MenuComponent.html#postEvent-java.awt.Event-), [setFont](../../java/awt/MenuComponent.html#setFont-java.awt.Font-), [setName](../../java/awt/MenuComponent.html#setName-java.lang.String-), [toString](../../java/awt/MenuComponent.html#toString--)` * ### Methods inherited from class java.lang.[Object](../../java/lang/Object.html "class in java.lang") `[clone](../../java/lang/Object.html#clone--), [equals](../../java/lang/Object.html#equals-java.lang.Object-), [finalize](../../java/lang/Object.html#finalize--), [getClass](../../java/lang/Object.html#getClass--), [hashCode](../../java/lang/Object.html#hashCode--), [notify](../../java/lang/Object.html#notify--), [notifyAll](../../java/lang/Object.html#notifyAll--), [wait](../../java/lang/Object.html#wait--), [wait](../../java/lang/Object.html#wait-long-), [wait](../../java/lang/Object.html#wait-long-int-)` * ### Methods inherited from interface java.awt.[MenuContainer](../../java/awt/MenuContainer.html "interface in java.awt") `[getFont](../../java/awt/MenuContainer.html#getFont--), [postEvent](../../java/awt/MenuContainer.html#postEvent-java.awt.Event-)`
Constructor Detail
* #### Menu public Menu() throws [HeadlessException](../../java/awt/HeadlessException.html "class in java.awt") Constructs a new menu with an empty label. This menu is not a tear-off menu. Throws: `[HeadlessException](../../java/awt/HeadlessException.html "class in java.awt")` \- if GraphicsEnvironment.isHeadless() returns true. Since: JDK1.1 See Also: [GraphicsEnvironment.isHeadless()](../../java/awt/GraphicsEnvironment.html#isHeadless--) * #### Menu public Menu([String](../../java/lang/String.html "class in java.lang") label) throws [HeadlessException](../../java/awt/HeadlessException.html "class in java.awt") Constructs a new menu with the specified label. This menu is not a tear-off menu. Parameters: `label` \- the menu's label in the menu bar, or in another menu of which this menu is a submenu. Throws: `[HeadlessException](../../java/awt/HeadlessException.html "class in java.awt")` \- if GraphicsEnvironment.isHeadless() returns true. See Also: [GraphicsEnvironment.isHeadless()](../../java/awt/GraphicsEnvironment.html#isHeadless--) * #### Menu public Menu([String](../../java/lang/String.html "class in java.lang") label, boolean tearOff) throws [HeadlessException](../../java/awt/HeadlessException.html "class in java.awt") Constructs a new menu with the specified label, indicating whether the menu can be torn off. Tear-off functionality may not be supported by all implementations of AWT. If a particular implementation doesn't support tear-off menus, this value is silently ignored. Parameters: `label` \- the menu's label in the menu bar, or in another menu of which this menu is a submenu. `tearOff` \- if `true`, the menu is a tear-off menu. Throws: `[HeadlessException](../../java/awt/HeadlessException.html "class in java.awt")` \- if GraphicsEnvironment.isHeadless() returns true. Since: JDK1.0. See Also: [GraphicsEnvironment.isHeadless()](../../java/awt/GraphicsEnvironment.html#isHeadless--)
Method Detail
* #### addNotify public void addNotify() Creates the menu's peer. The peer allows us to modify the appearance of the menu without changing its functionality. Overrides: `[addNotify](../../java/awt/MenuItem.html#addNotify--)` in class `[MenuItem](../../java/awt/MenuItem.html "class in java.awt")` * #### removeNotify public void removeNotify() Removes the menu's peer. The peer allows us to modify the appearance of the menu without changing its functionality. Overrides: `[removeNotify](../../java/awt/MenuComponent.html#removeNotify--)` in class `[MenuComponent](../../java/awt/MenuComponent.html "class in java.awt")` * #### isTearOff public boolean isTearOff() Indicates whether this menu is a tear-off menu. Tear-off functionality may not be supported by all implementations of AWT. If a particular implementation doesn't support tear-off menus, this value is silently ignored. Returns: `true` if this is a tear-off menu;`false` otherwise. * #### getItemCount public int getItemCount() Get the number of items in this menu. Returns: the number of items in this menu. Since: JDK1.1 * #### countItems [@Deprecated](../../java/lang/Deprecated.html "annotation in java.lang") public int countItems() Deprecated. * #### getItem public [MenuItem](../../java/awt/MenuItem.html "class in java.awt") getItem(int index) Gets the item located at the specified index of this menu. Parameters: `index` \- the position of the item to be returned. Returns: the item located at the specified index. * #### add public [MenuItem](../../java/awt/MenuItem.html "class in java.awt") add([MenuItem](../../java/awt/MenuItem.html "class in java.awt") mi) Adds the specified menu item to this menu. If the menu item has been part of another menu, removes it from that menu. Parameters: `mi` \- the menu item to be added Returns: the menu item added See Also: [insert(java.lang.String, int)](../../java/awt/Menu.html#insert-java.lang.String-int-), [insert(java.awt.MenuItem, int)](../../java/awt/Menu.html#insert-java.awt.MenuItem-int-) * #### add public void add([String](../../java/lang/String.html "class in java.lang") label) Adds an item with the specified label to this menu. Parameters: `label` \- the text on the item See Also: [insert(java.lang.String, int)](../../java/awt/Menu.html#insert-java.lang.String-int-), [insert(java.awt.MenuItem, int)](../../java/awt/Menu.html#insert-java.awt.MenuItem-int-) * #### insert public void insert([MenuItem](../../java/awt/MenuItem.html "class in java.awt") menuitem, int index) Inserts a menu item into this menu at the specified position. Parameters: `menuitem` \- the menu item to be inserted. `index` \- the position at which the menu item should be inserted. Throws: `[IllegalArgumentException](../../java/lang/IllegalArgumentException.html "class in java.lang")` \- if the value of`index` is less than zero Since: JDK1.1 See Also: [add(java.lang.String)](../../java/awt/Menu.html#add-java.lang.String-), [add(java.awt.MenuItem)](../../java/awt/Menu.html#add-java.awt.MenuItem-) * #### insert public void insert([String](../../java/lang/String.html "class in java.lang") label, int index) Inserts a menu item with the specified label into this menu at the specified position. This is a convenience method for`insert(menuItem, index)`. Parameters: `label` \- the text on the item `index` \- the position at which the menu item should be inserted Throws: `[IllegalArgumentException](../../java/lang/IllegalArgumentException.html "class in java.lang")` \- if the value of`index` is less than zero Since: JDK1.1 See Also: [add(java.lang.String)](../../java/awt/Menu.html#add-java.lang.String-), [add(java.awt.MenuItem)](../../java/awt/Menu.html#add-java.awt.MenuItem-) * #### addSeparator public void addSeparator() Adds a separator line, or a hypen, to the menu at the current position. See Also: [insertSeparator(int)](../../java/awt/Menu.html#insertSeparator-int-) * #### insertSeparator public void insertSeparator(int index) Inserts a separator at the specified position. Parameters: `index` \- the position at which the menu separator should be inserted. Throws: `[IllegalArgumentException](../../java/lang/IllegalArgumentException.html "class in java.lang")` \- if the value of`index` is less than 0. Since: JDK1.1 See Also: [addSeparator()](../../java/awt/Menu.html#addSeparator--) * #### remove public void remove(int index) Removes the menu item at the specified index from this menu. Parameters: `index` \- the position of the item to be removed. * #### remove public void remove([MenuComponent](../../java/awt/MenuComponent.html "class in java.awt") item) Removes the specified menu item from this menu. Specified by: `[remove](../../java/awt/MenuContainer.html#remove-java.awt.MenuComponent-)` in interface `[MenuContainer](../../java/awt/MenuContainer.html "interface in java.awt")` Parameters: `item` \- the item to be removed from the menu. If `item` is `null` or is not in this menu, this method does nothing. * #### removeAll public void removeAll() Removes all items from this menu. Since: JDK1.0. * #### paramString public [String](../../java/lang/String.html "class in java.lang") paramString() Returns a string representing the state of this `Menu`. 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`. Overrides: `[paramString](../../java/awt/MenuItem.html#paramString--)` in class `[MenuItem](../../java/awt/MenuItem.html "class in java.awt")` Returns: the parameter string of this menu * #### getAccessibleContext public [AccessibleContext](../../javax/accessibility/AccessibleContext.html "class in javax.accessibility") getAccessibleContext() Gets the AccessibleContext associated with this Menu. For menus, the AccessibleContext takes the form of an AccessibleAWTMenu. A new AccessibleAWTMenu instance is created if necessary. Specified by: `[getAccessibleContext](../../javax/accessibility/Accessible.html#getAccessibleContext--)` in interface `[Accessible](../../javax/accessibility/Accessible.html "interface in javax.accessibility")` Overrides: `[getAccessibleContext](../../java/awt/MenuItem.html#getAccessibleContext--)` in class `[MenuItem](../../java/awt/MenuItem.html "class in java.awt")` Returns: an AccessibleAWTMenu that serves as the AccessibleContext of this Menu Since: 1.3
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2025, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.