javax.swing

Class JMenuBar

Implemented Interfaces:
Accessible, ImageObserver, MenuContainer, MenuElement, Serializable
Known Direct 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

TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW, accessibleContext, listenerList, ui

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, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, 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, updateUI

Methods inherited from class java.awt.Container

add, add, add, add, add, addContainerListener, addImpl, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, extends EventListener> T[] getListeners, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponentZOrder, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, setLayout, transferFocusDownCycle, update, validate, validateTree

Methods inherited from class java.awt.Component

action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFocusable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFocusable, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

JMenuBar

public JMenuBar()
Creates a new JMenuBar object.

Method Details

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 interface JComponent

getAccessibleContext

public AccessibleContext getAccessibleContext()
Get the value of the accessibleContext property for this component.
Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in interface 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).

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 interface JComponent
Returns:
The Look and Feel classID. "MenuBarUI"

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 interface JComponent
Parameters:
g - The graphics context with which to paint the border

paramString

protected String paramString()
A string that describes this JMenuBar. Normally only used for debugging.
Overrides:
paramString in interface JComponent
Returns:
A string describing this JMenuBar

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 interface 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

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

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 interface JComponent

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 interface JComponent

JMenuBar.java -- Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.