- 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:
- JMenu,- JPopupMenu,- JMenuItem
- 
Nested Class SummaryNested Classes Modifier and Type Class Description protected classJMenuBar.AccessibleJMenuBarThis class implements accessibility support for theJMenuBarclass.Nested classes/interfaces declared in class java.awt.ComponentComponent.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
- 
Field SummaryFields declared in class javax.swing.JComponentlistenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWFields declared in class java.awt.ComponentaccessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTFields declared in interface java.awt.image.ImageObserverABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
- 
Constructor SummaryConstructors Constructor Description JMenuBar()Creates a new menu bar.
- 
Method SummaryModifier and Type Method Description JMenuadd(JMenu c)Appends the specified menu to the end of the menu bar.voidaddNotify()OverridesJComponent.addNotifyto register this menu bar with the current keyboard manager.AccessibleContextgetAccessibleContext()Gets the AccessibleContext associated with this JMenuBar.ComponentgetComponent()Implemented to be aMenuElement.ComponentgetComponentAtIndex(int i)Deprecated.replaced bygetComponent(int i)intgetComponentIndex(Component c)Returns the index of the specified component.JMenugetHelpMenu()Gets the help menu for the menu bar.InsetsgetMargin()Returns the margin between the menubar's border and its menus.JMenugetMenu(int index)Returns the menu at the specified position in the menu bar.intgetMenuCount()Returns the number of items in the menu bar.SingleSelectionModelgetSelectionModel()Returns the model object that handles single selections.MenuElement[]getSubElements()Implemented to be aMenuElement-- returns the menus in this menu bar.MenuBarUIgetUI()Returns the menubar's current UI.StringgetUIClassID()Returns the name of the L&F class that renders this component.booleanisBorderPainted()Returns true if the menu bars border should be painted.booleanisSelected()Returns true if the menu bar currently has a component selected.voidmenuSelectionChanged(boolean isIncluded)Implemented to be aMenuElement-- does nothing.protected voidpaintBorder(Graphics g)Paints the menubar's border ifBorderPaintedproperty is true.protected StringparamString()Returns a string representation of thisJMenuBar.protected booleanprocessKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)Subclassed to check all the child menus.voidprocessKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)Implemented to be aMenuElement-- does nothing.voidprocessMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)Implemented to be aMenuElement-- does nothing.voidremoveNotify()OverridesJComponent.removeNotifyto unregister this menu bar with the current keyboard manager.voidsetBorderPainted(boolean b)Sets whether the border should be painted.voidsetHelpMenu(JMenu menu)Sets the help menu that appears when the user selects the "help" option in the menu bar.voidsetMargin(Insets m)Sets the margin between the menubar's border and its menus.voidsetSelected(Component sel)Sets the currently selected component, producing a a change to the selection model.voidsetSelectionModel(SingleSelectionModel model)Sets the model object to handle single selections.voidsetUI(MenuBarUI ui)Sets the L&F object that renders this component.voidupdateUI()Resets the UI property with a value from the current look and feel.Methods declared in class javax.swing.JComponentaddAncestorListener, 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, updateMethods declared in class java.awt.Containeradd, 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, validateTreeMethods declared in class java.awt.Componentaction, 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- 
JMenuBarpublic JMenuBar()Creates a new menu bar.
 
- 
- 
Method Details- 
getUIReturns the menubar's current UI.- Overrides:
- getUIin class- JComponent
- Returns:
- a MenuBarUIwhich is the menubar's current L&F object
- See Also:
- setUI(javax.swing.plaf.MenuBarUI)
 
- 
setUI@BeanProperty(hidden=true, visualUpdate=true, description="The UI object that implements the Component\'s LookAndFeel.") public void setUI(MenuBarUI ui)Sets the L&F object that renders this component.- Parameters:
- ui- the new MenuBarUI L&F object
- See Also:
- UIDefaults.getUI(javax.swing.JComponent)
 
- 
updateUIpublic void updateUI()Resets the UI property with a value from the current look and feel.- Overrides:
- updateUIin class- JComponent
- See Also:
- JComponent.updateUI()
 
- 
getUIClassIDReturns the name of the L&F class that renders this component.- Overrides:
- getUIClassIDin class- JComponent
- Returns:
- the string "MenuBarUI"
- See Also:
- JComponent.getUIClassID(),- UIDefaults.getUI(javax.swing.JComponent)
 
- 
getSelectionModelReturns the model object that handles single selections.- Returns:
- the SingleSelectionModelproperty
- See Also:
- SingleSelectionModel
 
- 
setSelectionModel@BeanProperty(description="The selection model, recording which child is selected.") public void setSelectionModel(SingleSelectionModel model)Sets the model object to handle single selections.- Parameters:
- model- the- SingleSelectionModelto use
- See Also:
- SingleSelectionModel
 
- 
addAppends the specified menu to the end of the menu bar.- Parameters:
- c- the- JMenucomponent to add
- Returns:
- the menu component
 
- 
getMenuReturns the menu at the specified position in the menu bar.- 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)
 
- 
getMenuCountReturns the number of items in the menu bar.- Returns:
- the number of items in the menu bar
 
- 
setHelpMenuSets 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.- Parameters:
- menu- the JMenu that delivers help to the user
 
- 
getHelpMenuGets the help menu for the menu bar. This method is not yet implemented and will throw an exception.- Returns:
- the JMenuthat delivers help to the user
 
- 
getComponentAtIndexDeprecated.replaced bygetComponent(int i)Returns the component at the specified index.- Parameters:
- i- an integer specifying the position, where 0 is first
- Returns:
- the Componentat the position, ornullfor an invalid index
 
- 
getComponentIndexReturns the index of the specified component.- Parameters:
- c- the- Componentto find
- Returns:
- an integer giving the component's position, where 0 is first; or -1 if it can't be found
 
- 
setSelectedSets the currently selected component, producing a a change to the selection model.- Parameters:
- sel- the- Componentto select
 
- 
isSelectedReturns true if the menu bar currently has a component selected.- Returns:
- true if a selection has been made, else false
 
- 
isBorderPaintedpublic boolean isBorderPainted()Returns true if the menu bars border should be painted.- 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)Sets whether the border should be painted.- Parameters:
- b- if true and border property is not- null, the border is painted.
- See Also:
- isBorderPainted()
 
- 
paintBorderPaints the menubar's border ifBorderPaintedproperty is true.- Overrides:
- paintBorderin class- JComponent
- Parameters:
- g- the- Graphicscontext to use for painting
- See Also:
- JComponent.paint(java.awt.Graphics),- JComponent.setBorder(javax.swing.border.Border)
 
- 
setMargin@BeanProperty(visualUpdate=true, description="The space between the menubar\'s border and its contents") public void setMargin(Insets m)Sets the margin between the menubar's border and its menus. Setting tonullwill cause the menubar to use the default margins.- Parameters:
- m- an Insets object containing the margin values
- See Also:
- Insets
 
- 
getMarginReturns 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.- Returns:
- an Insetsobject containing the margin values
- See Also:
- Insets
 
- 
processMouseEventImplemented to be aMenuElement-- does nothing.- Specified by:
- processMouseEventin interface- MenuElement
- Parameters:
- event- a- MouseEventto be processed
- path- the path of the receiving element in the menu hierarchy
- manager- the- MenuSelectionManagerfor the menu hierarchy
- See Also:
- getSubElements()
 
- 
processKeyEventImplemented to be aMenuElement-- does nothing.- Specified by:
- processKeyEventin interface- MenuElement
- Parameters:
- e- a- KeyEventto be processed
- path- the path of the receiving element in the menu hierarchy
- manager- the- MenuSelectionManagerfor the menu hierarchy
- See Also:
- getSubElements()
 
- 
menuSelectionChangedpublic void menuSelectionChanged(boolean isIncluded)Implemented to be aMenuElement-- does nothing.- Specified by:
- menuSelectionChangedin interface- MenuElement
- Parameters:
- isIncluded- can be used to indicate if this- MenuElementis active (if it is a menu) or is on the part of the menu path that changed (if it is a menu item).
- See Also:
- getSubElements()
 
- 
getSubElementsImplemented to be aMenuElement-- returns the menus in this menu bar. This is the reason for implementing theMenuElementinterface -- so that the menu bar can be treated the same as other menu elements.- Specified by:
- getSubElementsin interface- MenuElement
- Returns:
- an array of menu items in the menu bar.
 
- 
getComponentImplemented to be aMenuElement. Returns this object.- Specified by:
- getComponentin interface- MenuElement
- Returns:
- the current Component(this)
- See Also:
- getSubElements()
 
- 
paramStringReturns a string representation of thisJMenuBar. 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 benull.- Overrides:
- paramStringin class- JComponent
- Returns:
- a string representation of this JMenuBar
 
- 
getAccessibleContextGets the AccessibleContext associated with this JMenuBar. For JMenuBars, the AccessibleContext takes the form of an AccessibleJMenuBar. A new AccessibleJMenuBar instance is created if necessary.- Specified by:
- getAccessibleContextin interface- Accessible
- Overrides:
- getAccessibleContextin class- Component
- Returns:
- an AccessibleJMenuBar that serves as the AccessibleContext of this JMenuBar
 
- 
processKeyBindingSubclassed to check all the child menus.- Overrides:
- processKeyBindingin class- JComponent
- Parameters:
- ks- the- KeyStrokequeried
- e- the- KeyEvent
- 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
 
- 
addNotifypublic void addNotify()OverridesJComponent.addNotifyto register this menu bar with the current keyboard manager.
- 
removeNotifypublic void removeNotify()OverridesJComponent.removeNotifyto unregister this menu bar with the current keyboard manager.
 
-