public class JMenuBar extends JComponent implements Accessible, MenuElement
An implementation of a menu bar. You add 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 XMLEncoder.
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.AccessibleJMenuBar This class implements accessibility support for the |
JComponent.AccessibleJComponent Container.AccessibleAWTContainer Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH public JMenuBar()
Creates a new menu bar.
public MenuBarUI getUI()
Returns the menubar's current UI.
setUI(javax.swing.plaf.MenuBarUI)public void setUI(MenuBarUI ui)
Sets the L&F object that renders this component.
ui - the new MenuBarUI L&F objectUIDefaults.getUI(javax.swing.JComponent)public void updateUI()
Resets the UI property with a value from the current look and feel.
updateUI in class JComponent
JComponent.updateUI()public String getUIClassID()
Returns the name of the L&F class that renders this component.
getUIClassID in class JComponent
JComponent.getUIClassID(), UIDefaults.getUI(javax.swing.JComponent)
public SingleSelectionModel getSelectionModel()
Returns the model object that handles single selections.
SingleSelectionModel propertySingleSelectionModelpublic void setSelectionModel(SingleSelectionModel model)
Sets the model object to handle single selections.
model - the SingleSelectionModel to useSingleSelectionModelpublic JMenu add(JMenu c)
Appends the specified menu to the end of the menu bar.
c - the JMenu component to addpublic JMenu getMenu(int index)
Returns the menu at the specified position in the menu bar.
index - an integer giving the position in the menu bar, where 0 is the first positionJMenu at that position, or null if if there is no JMenu at that position (ie. if it is a JMenuItem)public int getMenuCount()
Returns the number of items in the menu bar.
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.
menu - the JMenu that delivers help to the userpublic JMenu getHelpMenu()
Gets the help menu for the menu bar. This method is not yet implemented and will throw an exception.
JMenu that delivers help to the user@Deprecated public Component getComponentAtIndex(int i)
Deprecated. replaced by getComponent(int i)
Returns the component at the specified index.
i - an integer specifying the position, where 0 is firstComponent at the position, or null for an invalid indexpublic int getComponentIndex(Component c)
Returns the index of the specified component.
c - the Component to findpublic void setSelected(Component sel)
Sets the currently selected component, producing a a change to the selection model.
sel - the Component to selectpublic boolean isSelected()
Returns true if the menu bar currently has a component selected.
public boolean isBorderPainted()
Returns true if the menu bars border should be painted.
public void setBorderPainted(boolean b)
Sets whether the border should be painted.
b - if true and border property is not null, the border is painted.isBorderPainted()protected void paintBorder(Graphics g)
Paints the menubar's border if BorderPainted property is true.
paintBorder in class JComponent
g - the Graphics context to use for paintingJComponent.paint(java.awt.Graphics), JComponent.setBorder(javax.swing.border.Border)
public void setMargin(Insets m)
Sets the margin between the menubar's border and its menus. Setting to null will cause the menubar to use the default margins.
m - an Insets object containing the margin valuesInsetspublic 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.
Insets object containing the margin valuesInsetspublic void processMouseEvent(MouseEvent event,
MenuElement[] path,
MenuSelectionManager manager) Implemented to be a MenuElement -- does nothing.
processMouseEvent in interface MenuElement
getSubElements()public void processKeyEvent(KeyEvent e,
MenuElement[] path,
MenuSelectionManager manager) Implemented to be a MenuElement -- does nothing.
processKeyEvent in interface MenuElement
getSubElements()public void menuSelectionChanged(boolean isIncluded)
Implemented to be a MenuElement -- does nothing.
menuSelectionChanged in interface MenuElement
getSubElements()public MenuElement[] getSubElements()
Implemented to be a MenuElement -- returns the menus in this menu bar. This is the reason for implementing the MenuElement interface -- so that the menu bar can be treated the same as other menu elements.
getSubElements in interface MenuElement
public Component getComponent()
Implemented to be a MenuElement. Returns this object.
getComponent in interface MenuElement
Component (this)getSubElements()protected String paramString()
Returns a string representation of this JMenuBar. 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 JComponent
JMenuBar
public AccessibleContext getAccessibleContext()
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.
getAccessibleContext in interface Accessible
getAccessibleContext in class Component
protected boolean processKeyBinding(KeyStroke ks,
KeyEvent e,
int condition,
boolean pressed) Subclassed to check all the child menus.
processKeyBinding in class JComponent
ks - the KeyStroke queriede - the KeyEvent
condition - one of the following values: pressed - true if the key is pressedpublic void addNotify()
Overrides JComponent.addNotify to register this menu bar with the current keyboard manager.
addNotify in class JComponent
JComponent.registerKeyboardAction(java.awt.event.ActionListener, java.lang.String, javax.swing.KeyStroke, int)public void removeNotify()
Overrides JComponent.removeNotify to unregister this menu bar with the current keyboard manager.
removeNotify in class JComponent
JComponent.registerKeyboardAction(java.awt.event.ActionListener, java.lang.String, javax.swing.KeyStroke, int)
© 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.