Class JMenuBar

java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.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 classJMenuBarextendsJComponentimplementsAccessible,MenuElement
An implementation of a menu bar. You addJMenu objects to the menu bar to construct a menu. When the user selects aJMenu object, its associatedJPopupMenu is displayed, allowing the user to select one of theJMenuItems on it.

For information and examples of using menu bars seeHow to Use Menus, a section inThe Java Tutorial.

Warning: Swing is not thread safe. For more information seeSwing'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 thejava.beans package. Please seeXMLEncoder.

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 thefocusTraversalKeysEnabled property ofJMenuBar is set tofalse. To resolve this, you should call theJMenuBar.setFocusTraversalKeysEnabled(true) method.

Since:
1.2
See Also:
  • Constructor Details

    • JMenuBar

      public JMenuBar()
      Creates a new menu bar.
  • Method Details

    • getUI

      public MenuBarUI getUI()
      Returns the menubar's current UI.
      Overrides:
      getUI in class JComponent
      Returns:
      aMenuBarUI which 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)
      Sets the L&F object that renders this component.
      Parameters:
      ui - the new MenuBarUI L&F object
      See Also:
    • updateUI

      public void updateUI()
      Resets the UI property with a value from the current look and feel.
      Overrides:
      updateUI in class JComponent
      See Also:
    • getUIClassID

      @BeanProperty(bound=false)public String getUIClassID()
      Returns the name of the L&F class that renders this component.
      Overrides:
      getUIClassID in class JComponent
      Returns:
      the string "MenuBarUI"
      See Also:
    • getSelectionModel

      public SingleSelectionModel getSelectionModel()
      Returns the model object that handles single selections.
      Returns:
      theSingleSelectionModel property
      See Also:
    • 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 - theSingleSelectionModel to use
      See Also:
    • add

      public JMenu add(JMenu c)
      Appends the specified menu to the end of the menu bar.
      Parameters:
      c - theJMenu component to add
      Returns:
      the menu component
    • getMenu

      public JMenu getMenu(int index)
      Returns 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:
      theJMenu at that position, ornull if if there is noJMenu at that position (ie. if it is aJMenuItem)
    • getMenuCount

      @BeanProperty(bound=false)public int getMenuCount()
      Returns the number of items in the menu bar.
      Returns:
      the number of items in the menu bar
    • setHelpMenu

      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.
      Parameters:
      menu - the JMenu that delivers help to the user
    • getHelpMenu

      public JMenu getHelpMenu()
      Gets the help menu for the menu bar. This method is not yet implemented and will throw an exception.
      Returns:
      theJMenu that delivers help to the user
    • getComponentAtIndex

      @Deprecatedpublic Component getComponentAtIndex(int i)
      Deprecated.
      replaced bygetComponent(int i)
      Returns the component at the specified index.
      Parameters:
      i - an integer specifying the position, where 0 is first
      Returns:
      theComponent at the position, ornull for an invalid index
    • getComponentIndex

      public int getComponentIndex(Component c)
      Returns the index of the specified component.
      Parameters:
      c - theComponent to 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)
      Sets the currently selected component, producing a a change to the selection model.
      Parameters:
      sel - theComponent to select
    • isSelected

      @BeanProperty(bound=false)public boolean isSelected()
      Returns true if the menu bar currently has a component selected.
      Returns:
      true if a selection has been made, else false
    • isBorderPainted

      public 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 notnull, the border is painted.
      See Also:
    • paintBorder

      protected void paintBorder(Graphics g)
      Paints the menubar's border ifBorderPainted property is true.
      Overrides:
      paintBorder in class JComponent
      Parameters:
      g - theGraphics context 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)
      Sets the margin between the menubar's border and its menus. Setting tonull 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 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:
      anInsets object containing the margin values
      See Also:
    • processMouseEvent

      public void processMouseEvent(MouseEvent event,MenuElement[] path,MenuSelectionManager manager)
      Implemented to be aMenuElement -- does nothing.
      Specified by:
      processMouseEvent in interface MenuElement
      Parameters:
      event - aMouseEvent to be processed
      path - the path of the receiving element in the menu hierarchy
      manager - theMenuSelectionManager for the menu hierarchy
      See Also:
    • processKeyEvent

      public void processKeyEvent(KeyEvent e,MenuElement[] path,MenuSelectionManager manager)
      Implemented to be aMenuElement -- does nothing.
      Specified by:
      processKeyEvent in interface MenuElement
      Parameters:
      e - aKeyEvent to be processed
      path - the path of the receiving element in the menu hierarchy
      manager - theMenuSelectionManager for the menu hierarchy
      See Also:
    • menuSelectionChanged

      public void menuSelectionChanged(boolean isIncluded)
      Implemented to be aMenuElement -- does nothing.
      Specified by:
      menuSelectionChanged in interface MenuElement
      Parameters:
      isIncluded - can be used to indicate if thisMenuElement is 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

      @BeanProperty(bound=false)public MenuElement[] getSubElements()
      Implemented to be aMenuElement -- returns the menus in this menu bar. This is the reason for implementing theMenuElement interface -- so that the menu bar can be treated the same as other menu elements.
      Specified by:
      getSubElements in interface MenuElement
      Returns:
      an array of menu items in the menu bar.
    • getComponent

      public Component getComponent()
      Implemented to be aMenuElement. Returns this object.
      Specified by:
      getComponent in interface MenuElement
      Returns:
      the currentComponent (this)
      See Also:
    • paramString

      protected String paramString()
      Returns 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:
      paramString in class JComponent
      Returns:
      a string representation of thisJMenuBar
    • getAccessibleContext

      @BeanProperty(bound=false)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.
      Specified by:
      getAccessibleContext in interface Accessible
      Overrides:
      getAccessibleContext in class Component
      Returns:
      an AccessibleJMenuBar that serves as the AccessibleContext of this JMenuBar
    • processKeyBinding

      protected boolean processKeyBinding(KeyStroke ks,KeyEvent e, int condition, boolean pressed)
      Subclassed to check all the child menus.
      Overrides:
      processKeyBinding in class JComponent
      Parameters:
      ks - theKeyStroke queried
      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()
      OverridesJComponent.addNotify to register this menu bar with the current keyboard manager.
      Overrides:
      addNotify in class JComponent
      See Also:
    • removeNotify

      public void removeNotify()
      OverridesJComponent.removeNotify to unregister this menu bar with the current keyboard manager.
      Overrides:
      removeNotify in class JComponent
      See Also: