javax.swing
Class JMenuBar

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JMenuBar
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, MenuElement
Direct Known Subclasses:
BasicInternalFrameTitlePane.SystemMenuBar

public class JMenuBar
extends JComponent
implements Accessible, MenuElement

JMenuBar is a container for menu's. For a menu bar to be seen on the screen, at least one menu should be added to it. Just like adding components to container, one can use add() to add menu's to the menu bar. Menu's will be displayed in the menu bar in the order they were added. The JMenuBar uses selectionModel to keep track of selected menu index. JMenuBar's selectionModel will fire ChangeEvents to its registered listeners when the selected index changes.

See Also:
Serialized Form

Nested Class Summary
protected  class JMenuBar.AccessibleJMenuBar
          Provides accessibility support for JMenuBar.
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
JMenuBar()
          Creates a new JMenuBar object.
 
Method Summary
 JMenu add(JMenu c)
          Adds menu to the menu bar
 void addNotify()
          This method overrides addNotify() in the Container to register this menu bar with the current keyboard manager.
 AccessibleContext getAccessibleContext()
          Get the value of the accessibleContext property for this component.
 Component getComponent()
          Returns reference to this menu bar
 Component getComponentAtIndex(int i)
          Deprecated. Replaced by getComponent(int)
 int getComponentIndex(Component c)
          Returns index of the specified component
 JMenu getHelpMenu()
          This method is not implemented and will throw an Error if called.
 Insets getMargin()
          Returns the margin between the menu bar's border and its menus.
 JMenu getMenu(int index)
          Return menu at the specified index.
 int getMenuCount()
          Returns number of menu's in this menu bar
 SingleSelectionModel getSelectionModel()
          Returns selection model for this menu bar.
 MenuElement[] getSubElements()
          Method of MenuElement interface.
 MenuBarUI getUI()
          Set the "UI" property of the menu bar, which is a look and feel class responsible for handling the menuBar's input events and painting it.
 String getUIClassID()
          This method returns a name to identify which look and feel class will be the UI delegate for the menu bar.
 boolean isBorderPainted()
          Returns true if menu bar paints its border and false otherwise
 boolean isSelected()
          Returns true if some menu in menu bar is selected.
 void menuSelectionChanged(boolean isIncluded)
          This method does nothing by default.
protected  void paintBorder(Graphics g)
          Paints border of the menu bar, if its borderPainted property is set to true.
protected  String paramString()
          A string that describes this JMenuBar.
protected  boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
          This method overrides JComponent.processKeyBinding to allow the JMenuBar to check all the child components (recursiveley) to see if they'll consume the event.
 void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
          Process key events forwarded from MenuSelectionManager.
 void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)
          Process mouse events forwarded from MenuSelectionManager.
 void removeNotify()
          This method overrides removeNotify() in the Container to unregister this menu bar from the current keyboard manager.
 void setBorderPainted(boolean b)
          Sets painting status of the border.
 void setHelpMenu(JMenu menu)
          Sets help menu for this menu bar
 void setMargin(Insets m)
          Sets the margin between the menu bar's border and its menus (this is a bound property with the name 'margin').
 void setSelected(Component sel)
          Changes menu bar's selection to the specified menu.
 void setSelectionModel(SingleSelectionModel model)
          Sets menuBar's selection model to the one specified
 void setUI(MenuBarUI ui)
          Set the "UI" property of the menu bar, which is a look and feel class responsible for handling menuBar's input events and painting it.
 void updateUI()
          Set the "UI" property to a class constructed, via the UIManager, from the current look and feel.
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyEvent, 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, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setNextFocusableComponent, setOpaque, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from 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, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
 
Methods inherited from 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, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, 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, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JMenuBar

public JMenuBar()
Creates a new JMenuBar object.

Method Detail

add

public JMenu add(JMenu c)
Adds menu to the menu bar

Parameters:
c - menu to add
Returns:
reference to the added menu

addNotify

public void addNotify()
This method overrides addNotify() in the Container to register this menu bar with the current keyboard manager.

Overrides:
addNotify in class JComponent
See Also:
Component.isDisplayable(), Component.removeNotify()

getAccessibleContext

public AccessibleContext getAccessibleContext()
Description copied from class: JComponent
Get the value of the accessibleContext property for this component.

Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in class JComponent
Returns:
the current value of the property

getComponent

public Component getComponent()
Returns reference to this menu bar

Specified by:
getComponent in interface MenuElement
Returns:
reference to this menu bar

getComponentAtIndex

public Component getComponentAtIndex(int i)
Deprecated. Replaced by getComponent(int)

Returns component at the specified index.

Parameters:
i - index of the component to get
Returns:
component at the specified index. Null is returned if component at the specified index doesn't exist.

getComponentIndex

public int getComponentIndex(Component c)
Returns index of the specified component

Parameters:
c - Component to search for
Returns:
index of the specified component. -1 is returned if specified component doesnt' exist in the menu bar.

getHelpMenu

public JMenu getHelpMenu()
This method is not implemented and will throw an Error if called.

Returns:
This method never returns anything, it throws an exception.

getMargin

public Insets getMargin()
Returns the margin between the menu bar's border and its menus. If the margin is null, this method returns new Insets(0, 0, 0, 0).

Returns:
The margin (never null).
See Also:
setMargin(Insets)

getMenu

public JMenu getMenu(int index)
Return menu at the specified index. If component at the specified index is not a menu, then null is returned.

Parameters:
index - index to look for the menu
Returns:
menu at specified index, or null if menu doesn't exist at the specified index.

getMenuCount

public int getMenuCount()
Returns number of menu's in this menu bar

Returns:
number of menu's in this menu bar

getSelectionModel

public SingleSelectionModel getSelectionModel()
Returns selection model for this menu bar. SelectionModel keeps track of the selected menu in the menu bar. Whenever selected property of selectionModel changes, the ChangeEvent will be fired its ChangeListeners.

Returns:
selection model for this menu bar.

getSubElements

public MenuElement[] getSubElements()
Method of MenuElement interface. It returns subcomponents of the menu bar, which are all the menues that it contains.

Specified by:
getSubElements in interface MenuElement
Returns:
MenuElement[] array containing menues in this menu bar

getUI

public MenuBarUI getUI()
Set the "UI" property of the menu bar, which is a look and feel class responsible for handling the menuBar's input events and painting it.

Returns:
The current "UI" property

getUIClassID

public String getUIClassID()
This method returns a name to identify which look and feel class will be the UI delegate for the menu bar.

Overrides:
getUIClassID in class JComponent
Returns:
The Look and Feel classID. "MenuBarUI"
See Also:
JComponent.setUI(javax.swing.plaf.ComponentUI), JComponent.updateUI()

isBorderPainted

public boolean isBorderPainted()
Returns true if menu bar paints its border and false otherwise

Returns:
true if menu bar paints its border and false otherwise

isSelected

public boolean isSelected()
Returns true if some menu in menu bar is selected.

Returns:
true if some menu in menu bar is selected and false otherwise

menuSelectionChanged

public void menuSelectionChanged(boolean isIncluded)
This method does nothing by default. This method is need for the MenuElement interface to be implemented.

Specified by:
menuSelectionChanged in interface MenuElement
Parameters:
isIncluded - true if menuBar is included in the selection and false otherwise

paintBorder

protected void paintBorder(Graphics g)
Paints border of the menu bar, if its borderPainted property is set to true.

Overrides:
paintBorder in class JComponent
Parameters:
g - The graphics context with which to paint the border
See Also:
JComponent.paint(java.awt.Graphics), JComponent.paintChildren(java.awt.Graphics), JComponent.paintComponent(java.awt.Graphics)

paramString

protected String paramString()
A string that describes this JMenuBar. Normally only used for debugging.

Overrides:
paramString in class JComponent
Returns:
A string describing this JMenuBar

processKeyEvent

public void processKeyEvent(KeyEvent e,
                            MenuElement[] path,
                            MenuSelectionManager manager)
Process key events forwarded from MenuSelectionManager. This method doesn't do anything. It is here to conform to the MenuElement interface.

Specified by:
processKeyEvent in interface MenuElement
Parameters:
e - event forwarded from MenuSelectionManager
path - path to the menu element from which event was generated
manager - MenuSelectionManager for the current menu hierarchy

processKeyBinding

protected boolean processKeyBinding(KeyStroke ks,
                                    KeyEvent e,
                                    int condition,
                                    boolean pressed)
This method overrides JComponent.processKeyBinding to allow the JMenuBar to check all the child components (recursiveley) to see if they'll consume the event.

Overrides:
processKeyBinding in class JComponent
Parameters:
ks - the KeyStroke for the event
e - the KeyEvent for the event
condition - the focus condition for the binding
pressed - true if the key is pressed

processMouseEvent

public void processMouseEvent(MouseEvent event,
                              MenuElement[] path,
                              MenuSelectionManager manager)
Process mouse events forwarded from MenuSelectionManager. This method doesn't do anything. It is here to conform to the MenuElement interface.

Specified by:
processMouseEvent in interface MenuElement
Parameters:
event - event forwarded from MenuSelectionManager
path - path to the menu element from which event was generated
manager - MenuSelectionManager for the current menu hierarchy

removeNotify

public void removeNotify()
This method overrides removeNotify() in the Container to unregister this menu bar from the current keyboard manager.

Overrides:
removeNotify in class JComponent
See Also:
Component.isDisplayable(), Component.addNotify()

setBorderPainted

public void setBorderPainted(boolean b)
Sets painting status of the border. If 'b' is true then menu bar's border will be painted, and it will not be painted otherwise.

Parameters:
b - indicates if menu bar's border should be painted.

setHelpMenu

public void setHelpMenu(JMenu menu)
Sets help menu for this menu bar

Parameters:
menu - help menu

setMargin

public void setMargin(Insets m)
Sets the margin between the menu bar's border and its menus (this is a bound property with the name 'margin').

Parameters:
m - the margin (null permitted).
See Also:
getMargin()

setSelected

public void setSelected(Component sel)
Changes menu bar's selection to the specified menu. This method updates selected index of menu bar's selection model, which results in a model firing change event.

Parameters:
sel - menu to select

setSelectionModel

public void setSelectionModel(SingleSelectionModel model)
Sets menuBar's selection model to the one specified

Parameters:
model - SingleSelectionModel that needs to be set for this menu bar

setUI

public void setUI(MenuBarUI ui)
Set the "UI" property of the menu bar, which is a look and feel class responsible for handling menuBar's input events and painting it.

Parameters:
ui - The new "UI" property

updateUI

public void updateUI()
Set the "UI" property to a class constructed, via the UIManager, from the current look and feel.

Overrides:
updateUI in class JComponent