JMenu objects to the
menu bar to construct a menu. When the user selects a JMenu
object, its associated JPopupMenu is displayed, allowing the
user to select one of the JMenuItems on it.
For information and examples of using menu bars 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 java..
Warning
By default, pressing the Tab key does not transfer focus from a
JMenuBar which is added to a container together with other Swing
components, because the focusTraversalKeysEnabled property
of JMenuBar is set to false. To resolve this,
you should call the JMenuBar.setFocusTraversalKeysEnabled(true)
method.
JMenu, JPopupMenu, JMenuItem
| Modifier and Type | Class and Description |
|---|---|
| protected class | JMenuBar.
This class implements accessibility support for the
|
| Modifier and Type | Field and Description |
|---|---|
| private static final boolean | |
| private Insets | |
| private boolean | |
| private transient SingleSelectionModel | |
| private static final boolean | |
| private static final String | |
| private static final boolean |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public JMenu | |
| public void | addNotify()
Overrides javax. Overrides |
| public AccessibleContext | Returns: an AccessibleJMenuBar that serves as the AccessibleContext of this JMenuBarOverrides java. Implements javax. Gets the AccessibleContext associated with this JMenuBar. |
| public Component | Returns: the currentComponent (this)Implements javax. Implemented to be a |
| public Component | Returns: theComponent at the position,
or null for an invalid indexan integer specifying the position, where 0 is first i)
Deprecated
replaced by
getComponent(int i)
Returns the component at the specified index. |
| public int | Returns: an integer giving the component's position, where 0 is first; or -1 if it can't be foundthe c)Component to findReturns the index of the specified component. |
| public JMenu | |
| public Insets | Returns: anInsets object containing the margin valuesReturns the margin between the menubar's border and its menus. |
| public JMenu | Returns: theJMenu at that position, or null if
if there is no JMenu at that position (ie. if
it is a JMenuItem)an integer giving the position in the menu bar, where
0 is the first position index)Returns the menu at the specified position in the menu bar. |
| public int | Returns: the number of items in the menu barReturns the number of items in the menu bar. |
| public SingleSelectionModel | Returns: theSingleSelectionModel propertyReturns the model object that handles single selections. |
| public MenuElement[] | Returns: an array of menu items in the menu bar.Implements javax. Implemented to be a |
| public MenuBarUI | Returns: aMenuBarUI which is the menubar's current L&F objectOverrides javax. Returns the menubar's current UI. |
| public String | Returns: the string "MenuBarUI"Overrides javax. Returns the name of the L&F class that renders this component. |
| public boolean | Returns: true if the border should be painted, else falseReturns true if the menu bars border should be painted. |
| public boolean | Returns: true if a selection has been made, else falseReturns true if the menu bar currently has a component selected. |
| public void | menuSelectionChanged(boolean isIncluded)
Implements javax. Implemented to be a |
| protected void | paintBorder(Graphics
the g)Graphics context to use for paintingOverrides javax. Paints the menubar's border if |
| protected String | Returns: a string representation of thisJMenuBarOverrides javax. Returns a string representation of this |
| pack-priv static boolean | processBindingForKeyStrokeRecursive(MenuElement elem, KeyStroke ks, KeyEvent e, int condition, boolean pressed)
|
| protected boolean | processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
Overrides javax. Subclassed to check all the child menus. |
| public void | processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
Implements javax. Implemented to be a |
| public void | processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)
Implements javax. Implemented to be a |
| private void | readObject(ObjectInputStream s)
Hides javax. See JComponent.readObject() for information about serialization in Swing. |
| public void | removeNotify()
Overrides javax. Overrides |
| public void | setBorderPainted(boolean
if true and border property is not b)null,
the border is painted.Sets whether the border should be painted. |
| public void | setHelpMenu(JMenu
the JMenu that delivers help to the user menu)Sets the help menu that appears when the user selects the "help" option in the menu bar. |
| public void | |
| public void | setSelected(Component
the sel)Component to selectSets the currently selected component, producing a a change to the selection model. |
| public void | setSelectionModel(SingleSelectionModel
the model)SingleSelectionModel to useSets the model object to handle single selections. |
| public void | |
| public void | updateUI()
Overrides javax. Resets the UI property with a value from the current look and feel. |
| private void | writeObject(ObjectOutputStream s)
Hides javax. Before writing a |
| DEBUG | back to summary |
|---|---|
| private static final boolean DEBUG | |
| margin | back to summary |
|---|---|
| private Insets margin | |
| paintBorder | back to summary |
|---|---|
| private boolean paintBorder | |
| selectionModel | back to summary |
|---|---|
| private transient SingleSelectionModel selectionModel | |
| TRACE | back to summary |
|---|---|
| private static final boolean TRACE | |
| uiClassID | back to summary |
|---|---|
| private static final String uiClassID Hides javax.
| |
| VERBOSE | back to summary |
|---|---|
| private static final boolean VERBOSE | |
| JMenuBar | back to summary |
|---|---|
| public JMenuBar() Creates a new menu bar. | |
| add | back to summary |
|---|---|
| public JMenu add(JMenu c) Appends the specified menu to the end of the menu bar. | |
| addNotify | back to summary |
|---|---|
| public void addNotify() Overrides javax. Overrides | |
| getAccessibleContext | back to summary |
|---|---|
| public AccessibleContext getAccessibleContext() Overrides java. Implements javax. Gets the AccessibleContext associated with this JMenuBar. For JMenuBars, the AccessibleContext takes the form of an AccessibleJMenuBar. A new AccessibleJMenuBar instance is created if necessary.
| |
| getComponent | back to summary |
|---|---|
| public Component getComponent() Implements javax. Implemented to be a
| |
| getComponentAtIndex | back to summary |
|---|---|
| public Component getComponentAtIndex(int i)
Deprecated replaced by Returns the component at the specified index.
| |
| getComponentIndex | back to summary |
|---|---|
| public int getComponentIndex(Component c) Returns the index of the specified component.
| |
| getHelpMenu | back to summary |
|---|---|
| public JMenu getHelpMenu() Gets the help menu for the menu bar. This method is not yet implemented and will throw an exception.
| |
| getMargin | back to summary |
|---|---|
| public Insets getMargin() Returns the margin between the menubar's border and its menus. If there is no previous margin, it will create a default margin with zero size. | |
| getMenu | back to summary |
|---|---|
| public JMenu getMenu(int index) Returns the menu at the specified position in the menu bar.
| |
| getMenuCount | back to summary |
|---|---|
| public int getMenuCount() Returns the number of items in the menu bar.
| |
| getSelectionModel | back to summary |
|---|---|
| public SingleSelectionModel getSelectionModel() Returns the model object that handles single selections.
| |
| getSubElements | back to summary |
|---|---|
| public MenuElement[] getSubElements() Implements javax. Implemented to be a
| |
| getUI | back to summary |
|---|---|
| public MenuBarUI getUI() Overrides javax. Returns the menubar's current UI. | |
| getUIClassID | back to summary |
|---|---|
| public String getUIClassID() Overrides javax. Returns the name of the L&F class that renders this component.
| |
| isBorderPainted | back to summary |
|---|---|
| public boolean isBorderPainted() Returns true if the menu bars border should be painted.
| |
| isSelected | back to summary |
|---|---|
| public boolean isSelected() Returns true if the menu bar currently has a component selected.
| |
| menuSelectionChanged | back to summary |
|---|---|
| public void menuSelectionChanged(boolean isIncluded) Implements javax. Implemented to be a
| |
| paintBorder | back to summary |
|---|---|
| protected void paintBorder(Graphics g) Overrides javax. Paints the menubar's border if
| |
| paramString | back to summary |
|---|---|
| protected String paramString() Overrides javax. Returns a string representation of this
| |
| processBindingForKeyStrokeRecursive | back to summary |
|---|---|
| pack-priv static boolean processBindingForKeyStrokeRecursive(MenuElement elem, KeyStroke ks, KeyEvent e, int condition, boolean pressed) | |
| processKeyBinding | back to summary |
|---|---|
| protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) Overrides javax. Subclassed to check all the child menus.
| |
| processKeyEvent | back to summary |
|---|---|
| public void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager) Implements javax. Implemented to be a
| |
| processMouseEvent | back to summary |
|---|---|
| public void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager) Implements javax. Implemented to be a
| |
| readObject | back to summary |
|---|---|
| private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException Hides javax. See JComponent.readObject() for information about serialization in Swing.
| |
| removeNotify | back to summary |
|---|---|
| public void removeNotify() Overrides javax. Overrides | |
| setBorderPainted | back to summary |
|---|---|
| public void setBorderPainted(boolean b) Sets whether the border should be painted.
| |
| setHelpMenu | back to summary |
|---|---|
| public void setHelpMenu(JMenu menu) Sets the help menu that appears when the user selects the "help" option in the menu bar. This method is not yet implemented and will throw an exception.
| |
| setMargin | back to summary |
|---|---|
| public void setMargin(Insets m) Sets the margin between the menubar's border and
its menus. Setting to
| |
| setSelected | back to summary |
|---|---|
| public void setSelected(Component sel) Sets the currently selected component, producing a a change to the selection model.
| |
| setSelectionModel | back to summary |
|---|---|
| public void setSelectionModel(SingleSelectionModel model) Sets the model object to handle single selections.
| |
| setUI | back to summary |
|---|---|
| public void setUI(MenuBarUI ui) Sets the L&F object that renders this component.
| |
| updateUI | back to summary |
|---|---|
| public void updateUI() Overrides javax. Resets the UI property with a value from the current look and feel.
| |
| writeObject | back to summary |
|---|---|
| private void writeObject(ObjectOutputStream s) throws IOException Hides javax. Doc from javax. Before writing a
| |
JMenuBar class. It provides an implementation of the
Java Accessibility API appropriate to menu bar user-interface
elements.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 java..
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public void | addAccessibleSelection(int
the zero-based index of selectable items i)Implements javax. Selects the nth menu in the menu bar, forcing it to pop up. |
| public void | clearAccessibleSelection()
Implements javax. Clears the selection in the object, so that nothing in the object is selected. |
| public AccessibleRole | Returns: an instance of AccessibleRole describing the role of the objectOverrides javax. Get the role of this object. |
| public AccessibleSelection | Returns: this objectOverrides javax. Get the AccessibleSelection associated with this object. |
| public Accessible | getAccessibleSelection(int i)
Implements javax. Returns the currently selected menu if one is selected, otherwise null. |
| public int | Returns: 1 if a menu is currently selected, else 0Implements javax. Returns 1 if a menu is currently selected in this menu bar. |
| public AccessibleStateSet | Returns: an instance of AccessibleState containing the current state of the objectOverrides javax. Get the accessible state set of this object. |
| public boolean | isAccessibleChildSelected(int
the zero-based index of the child in this Accessible
object. i)Implements javax. Returns true if the current child of this object is selected. |
| public void | removeAccessibleSelection(int
the zero-based index of selectable items i)Implements javax. Removes the nth selected item in the object from the object's selection. |
| public void | selectAllAccessibleSelection()
Implements javax. Normally causes every selected item in the object to be selected if the object supports multiple selections. |
| AccessibleJMenuBar | back to summary |
|---|---|
| protected AccessibleJMenuBar() Constructs an | |
| addAccessibleSelection | back to summary |
|---|---|
| public void addAccessibleSelection(int i) Implements javax. Selects the nth menu in the menu bar, forcing it to pop up. If another menu is popped up, this will force it to close. If the nth menu is already selected, this method has no effect.
| |
| clearAccessibleSelection | back to summary |
|---|---|
| public void clearAccessibleSelection() Implements javax. Clears the selection in the object, so that nothing in the object is selected. This will close any open menu. | |
| getAccessibleRole | back to summary |
|---|---|
| public AccessibleRole getAccessibleRole() Overrides javax. Get the role of this object.
| |
| getAccessibleSelection | back to summary |
|---|---|
| public AccessibleSelection getAccessibleSelection() Overrides javax. Get the AccessibleSelection associated with this object. In the implementation of the Java Accessibility API for this class, return this object, which is responsible for implementing the AccessibleSelection interface on behalf of itself.
| |
| getAccessibleSelection | back to summary |
|---|---|
| public Accessible getAccessibleSelection(int i) Implements javax. Returns the currently selected menu if one is selected, otherwise null. | |
| getAccessibleSelectionCount | back to summary |
|---|---|
| public int getAccessibleSelectionCount() Implements javax. Returns 1 if a menu is currently selected in this menu bar.
| |
| getAccessibleStateSet | back to summary |
|---|---|
| public AccessibleStateSet getAccessibleStateSet() Overrides javax. Get the accessible state set of this object.
| |
| isAccessibleChildSelected | back to summary |
|---|---|
| public boolean isAccessibleChildSelected(int i) Implements javax. Returns true if the current child of this object is selected.
| |
| removeAccessibleSelection | back to summary |
|---|---|
| public void removeAccessibleSelection(int i) Implements javax. Removes the nth selected item in the object from the object's selection. If the nth item isn't currently selected, this method has no effect. Otherwise, it closes the popup menu.
| |
| selectAllAccessibleSelection | back to summary |
|---|---|
| public void selectAllAccessibleSelection() Implements javax. Normally causes every selected item in the object to be selected if the object supports multiple selections. This method makes no sense in a menu bar, and so does nothing. | |