Class JMenuBar
- All Implemented Interfaces:
-
ImageObserver,MenuContainer,Serializable,Accessible,MenuElement
- Direct Known Subclasses:
BasicInternalFrameTitlePane.SystemMenuBar
@JavaBean(defaultProperty="UI", description="A container for holding and displaying menus.") public class JMenuBar extends JComponent implements Accessible, MenuElement
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.
- Since:
- 1.2
- See Also:
Nested Class Summary
| Modifier and Type | Class | Description |
|---|---|---|
protected class |
JMenuBar.AccessibleJMenuBar |
This class implements accessibility support for the JMenuBar class. |
Nested classes/interfaces declared in class javax.swing.JComponent
JComponent.AccessibleJComponent
Nested classes/interfaces declared in class java.awt.Container
Container.AccessibleAWTContainer
Nested classes/interfaces declared in class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
Field Summary
Fields declared in class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Fields declared in class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
Fields declared in interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor Summary
| Constructor | Description |
|---|---|
JMenuBar() |
Creates a new menu bar. |
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
JMenu |
add |
Appends the specified menu to the end of the menu bar. |
void |
addNotify() |
Overrides JComponent.addNotify to register this menu bar with the current keyboard manager. |
AccessibleContext |
getAccessibleContext() |
Gets the AccessibleContext associated with this JMenuBar. |
Component |
getComponent() |
Implemented to be a MenuElement. |
Component |
getComponentAtIndex |
Deprecated. |
int |
getComponentIndex |
Returns the index of the specified component. |
JMenu |
getHelpMenu() |
Gets the help menu for the menu bar. |
Insets |
getMargin() |
Returns the margin between the menubar's border and its menus. |
JMenu |
getMenu |
Returns the menu at the specified position in the menu bar. |
int |
getMenuCount() |
Returns the number of items in the menu bar. |
SingleSelectionModel |
getSelectionModel() |
Returns the model object that handles single selections. |
MenuElement[] |
getSubElements() |
Implemented to be a MenuElement -- returns the menus in this menu bar. |
MenuBarUI |
getUI() |
Returns the menubar's current UI. |
String |
getUIClassID() |
Returns the name of the L&F class that renders this component. |
boolean |
isBorderPainted() |
Returns true if the menu bars border should be painted. |
boolean |
isSelected() |
Returns true if the menu bar currently has a component selected. |
void |
menuSelectionChanged |
Implemented to be a MenuElement -- does nothing. |
protected void |
paintBorder |
Paints the menubar's border if BorderPainted property is true. |
protected String |
paramString() |
Returns a string representation of this JMenuBar. |
protected boolean |
processKeyBinding |
Subclassed to check all the child menus. |
void |
processKeyEvent |
Implemented to be a MenuElement -- does nothing. |
void |
processMouseEvent |
Implemented to be a MenuElement -- does nothing. |
void |
removeNotify() |
Overrides JComponent.removeNotify to unregister this menu bar with the current keyboard manager. |
void |
setBorderPainted |
Sets whether the border should be painted. |
void |
setHelpMenu |
Sets the help menu that appears when the user selects the "help" option in the menu bar. |
void |
setMargin |
Sets the margin between the menubar's border and its menus. |
void |
setSelected |
Sets the currently selected component, producing a a change to the selection model. |
void |
setSelectionModel |
Sets the model object to handle single selections. |
void |
setUI |
Sets the L&F object that renders this component. |
void |
updateUI() |
Resets the UI property with a value from the current look and feel. |
Methods declared in class javax.swing.JComponent
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
Methods declared in class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
Methods declared in class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
Constructor Details
JMenuBar
public JMenuBar()
Method Details
getUI
public MenuBarUI getUI()
- Overrides:
-
getUIin classJComponent - Returns:
- a
MenuBarUIwhich is the menubar's current L&F object - See Also:
setUI
@BeanProperty(hidden=true, visualUpdate=true, description="The UI object that implements the Component\'s LookAndFeel.") public void setUI(MenuBarUI ui)
- Parameters:
-
ui- the new MenuBarUI L&F object - See Also:
updateUI
public void updateUI()
- Overrides:
-
updateUIin classJComponent - See Also:
getUIClassID
@BeanProperty(bound=false) public String getUIClassID()
- Overrides:
-
getUIClassIDin classJComponent - Returns:
- the string "MenuBarUI"
- See Also:
getSelectionModel
public SingleSelectionModel getSelectionModel()
- Returns:
- the
SingleSelectionModelproperty - See Also:
setSelectionModel
@BeanProperty(description="The selection model, recording which child is selected.") public void setSelectionModel(SingleSelectionModel model)
- Parameters:
-
model- theSingleSelectionModelto use - See Also:
add
public JMenu add(JMenu c)
- Parameters:
-
c- theJMenucomponent to add - Returns:
- the menu component
getMenu
public JMenu getMenu(int index)
- Parameters:
-
index- an integer giving the position in the menu bar, where 0 is the first position - Returns:
- the
JMenuat that position, ornullif if there is noJMenuat that position (ie. if it is aJMenuItem)
getMenuCount
@BeanProperty(bound=false) public int getMenuCount()
- Returns:
- the number of items in the menu bar
setHelpMenu
public void setHelpMenu(JMenu menu)
- Parameters:
-
menu- the JMenu that delivers help to the user
getHelpMenu
public JMenu getHelpMenu()
- Returns:
- the
JMenuthat delivers help to the user
getComponentAtIndex
@Deprecated public Component getComponentAtIndex(int i)
getComponent(int i)
- Parameters:
-
i- an integer specifying the position, where 0 is first - Returns:
- the
Componentat the position, ornullfor an invalid index
getComponentIndex
public int getComponentIndex(Component c)
- Parameters:
-
c- theComponentto find - Returns:
- an integer giving the component's position, where 0 is first; or -1 if it can't be found
setSelected
public void setSelected(Component sel)
- Parameters:
-
sel- theComponentto select
isSelected
@BeanProperty(bound=false) public boolean isSelected()
- Returns:
- true if a selection has been made, else false
isBorderPainted
public boolean isBorderPainted()
- Returns:
- true if the border should be painted, else false
setBorderPainted
@BeanProperty(visualUpdate=true, description="Whether the border should be painted.") public void setBorderPainted(boolean b)
- Parameters:
-
b- if true and border property is notnull, the border is painted. - See Also:
paintBorder
protected void paintBorder(Graphics g)
BorderPainted property is true.- Overrides:
-
paintBorderin classJComponent - Parameters:
-
g- theGraphicscontext to use for painting - See Also:
setMargin
@BeanProperty(visualUpdate=true, description="The space between the menubar\'s border and its contents") public void setMargin(Insets m)
null will cause the menubar to use the default margins.- Parameters:
-
m- an Insets object containing the margin values - See Also:
getMargin
public Insets getMargin()
- Returns:
- an
Insetsobject containing the margin values - See Also:
processMouseEvent
public void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)
MenuElement -- does nothing.- Specified by:
-
processMouseEventin interfaceMenuElement - Parameters:
-
event- aMouseEventto be processed -
path- the path of the receiving element in the menu hierarchy -
manager- theMenuSelectionManagerfor the menu hierarchy - See Also:
processKeyEvent
public void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
MenuElement -- does nothing.- Specified by:
-
processKeyEventin interfaceMenuElement - Parameters:
-
e- aKeyEventto be processed -
path- the path of the receiving element in the menu hierarchy -
manager- theMenuSelectionManagerfor the menu hierarchy - See Also:
getSubElements
@BeanProperty(bound=false) public MenuElement[] getSubElements()
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.- Specified by:
-
getSubElementsin interfaceMenuElement - Returns:
- an array of menu items in the menu bar.
getComponent
public Component getComponent()
MenuElement. Returns this object.- Specified by:
-
getComponentin interfaceMenuElement - Returns:
- the current
Component(this) - See Also:
paramString
protected String paramString()
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.- Overrides:
-
paramStringin classJComponent - Returns:
- a string representation of this
JMenuBar
getAccessibleContext
@BeanProperty(bound=false) public AccessibleContext getAccessibleContext()
- Specified by:
-
getAccessibleContextin interfaceAccessible - Overrides:
-
getAccessibleContextin classComponent - Returns:
- an AccessibleJMenuBar that serves as the AccessibleContext of this JMenuBar
processKeyBinding
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
- Overrides:
-
processKeyBindingin classJComponent - Parameters:
-
ks- theKeyStrokequeried -
e- theKeyEvent -
condition- one of the following values:- JComponent.WHEN_FOCUSED
- JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
- JComponent.WHEN_IN_FOCUSED_WINDOW
-
pressed- true if the key is pressed - Returns:
- true if there was a binding to an action, and the action was enabled
- Since:
- 1.3
addNotify
public void addNotify()
JComponent.addNotify to register this menu bar with the current keyboard manager.removeNotify
public void removeNotify()
JComponent.removeNotify to unregister this menu bar with the current keyboard manager.
© 1993, 2021, 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.
https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/JMenuBar.html
getComponent(int i)