This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.


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:
MenuContainer, ImageObserver, Serializable, Accessible, MenuElement, TransferHandler.HasGetTransferHandler
Direct Known Subclasses:
BasicInternalFrameTitlePane.SystemMenuBar

Most common way to construct:

JMenuBar menuBar = new JMenuBar();

Based on 161 examples


public class JMenuBar
extends JComponent
implements Accessible, MenuElement

An implementation of a menu bar. You add 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 JavaBeansTM has been added to the java.beans package. Please see {@link java.beans.XMLEncoder}.


Nested Class Summary
protected class

           This class implements accessibility support for the JMenuBar class.
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.BaselineResizeBehavior, 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
 
Constructor Summary

          Creates a new menu bar.
 
Method Summary
 JMenu

          Appends the specified menu to the end of the menu bar.
 void

          Overrides JComponent.addNotify to register this menu bar with the current keyboard manager.
 AccessibleContext

          Gets the AccessibleContext associated with this JMenuBar.
 Component

          Implemented to be a MenuElement.
 Component

          Returns the component at the specified index.
 int

          Returns the index of the specified component.
 JMenu

          Gets the help menu for the menu bar.
 Insets

          Returns the margin between the menubar's border and its menus.
 JMenu
getMenu(int index)

          Returns the menu at the specified position in the menu bar.
 int

          Returns the number of items in the menu bar.
 SingleSelectionModel

          Returns the model object that handles single selections.
 MenuElement[]

          Implemented to be a MenuElement -- returns the menus in this menu bar.
 MenuBarUI

          Returns the menubar's current UI.
 String

          Returns the name of the L&F class that renders this component.
 boolean

          Returns true if the menu bars border should be painted.
 boolean

          Returns true if the menu bar currently has a component selected.
 void
menuSelectionChanged(boolean isIncluded)

          Implemented to be a MenuElement -- does nothing.
protected void

          Paints the menubar's border if BorderPainted property is true.
protected String

          Returns a string representation of this JMenuBar.
protected boolean
processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)

          Subclassed to check all the child menus.
 void

          Implemented to be a MenuElement -- does nothing.
 void

          Implemented to be a MenuElement -- does nothing.
 void

          Overrides JComponent.removeNotify to unregister this menu bar with the current keyboard manager.
 void
setBorderPainted(boolean b)

          Sets whether the border should be painted.
 void

          Sets the help menu that appears when the user selects the "help" option in the menu bar.
 void

          Sets the margin between the menubar's border and its menus.
 void

          Sets the currently selected component, producing a a change to the selection model.
 void

          Sets the model object to handle single selections.
 void

          Sets the L&F object that renders this component.
 void

          Resets the UI property with a value from the current look and feel.
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, 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, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, 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, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, 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, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getMousePosition, 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, transferFocusBackward, 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, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getAlignmentX, getAlignmentY, getBackground, getBaseline, getBaselineResizeBehavior, 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, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, 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, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, 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, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, 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, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JMenuBar

public JMenuBar()
Creates a new menu bar.

Method Detail

add

public JMenu add(JMenu c)
Appends the specified menu to the end of the menu bar.

Parameters:
c - the JMenu component to add
Returns:
the menu component

addNotify

public void addNotify()
Overrides JComponent.addNotify to register this menu bar with the current keyboard manager.

Overrides:
addNotify in class JComponent

getAccessibleContext

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.

Overrides:
getAccessibleContext in class JComponent
Returns:
an AccessibleJMenuBar that serves as the AccessibleContext of this JMenuBar

getComponent

public Component getComponent()
Implemented to be a MenuElement. Returns this object.

Returns:
the current Component (this)

getComponentAtIndex

public Component getComponentAtIndex(int i)
Returns the component at the specified index.

Parameters:
i - an integer specifying the position, where 0 is first
Returns:
the Component at the position, or null for an invalid index

getComponentIndex

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

Parameters:
c - the Component to find
Returns:
an integer giving the component's position, where 0 is first; or -1 if it can't be found

getHelpMenu

public JMenu getHelpMenu()
Gets the help menu for the menu bar. This method is not yet implemented and will throw an exception.

Returns:
the JMenu that delivers help to the user

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:
an Insets object containing the margin values

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:
the JMenu at that position, or null if if there is no JMenu at that position (ie. if it is a JMenuItem)

getMenuCount

public int getMenuCount()
Returns the number of items in the menu bar.

Returns:
the number of items in the menu bar

getSelectionModel

public SingleSelectionModel getSelectionModel()
Returns the model object that handles single selections.

Returns:
the SingleSelectionModel property

getSubElements

public MenuElement[] getSubElements()
Implemented to be a 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.

Returns:
an array of menu items in the menu bar.

getUI

public MenuBarUI getUI()
Returns the menubar's current UI.


getUIClassID

public String getUIClassID()
Returns the name of the L&F class that renders this component.

Overrides:
getUIClassID in class JComponent
Returns:
the string "MenuBarUI"

isBorderPainted

public boolean isBorderPainted()
Returns true if the menu bars border should be painted.

Returns:
true if the border should be painted, else false

isSelected

public boolean isSelected()
Returns true if the menu bar currently has a component selected.

Returns:
true if a selection has been made, else false

menuSelectionChanged

public void menuSelectionChanged(boolean isIncluded)
Implemented to be a MenuElement -- does nothing.

Parameters:
isIncluded

paintBorder

protected void paintBorder(Graphics g)
Paints the menubar's border if BorderPainted property is true.

Overrides:
paintBorder in class JComponent
Parameters:
g - the Graphics context to use for painting

paramString

protected String paramString()
Returns a string representation of this 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:
paramString in class JComponent
Returns:
a string representation 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
e
condition
pressed

processKeyEvent

public void processKeyEvent(KeyEvent e,
                            MenuElement[] path,
                            MenuSelectionManager manager)
Implemented to be a MenuElement -- does nothing.

Parameters:
e
path
manager

processMouseEvent

public void processMouseEvent(MouseEvent event,
                              MenuElement[] path,
                              MenuSelectionManager manager)
Implemented to be a MenuElement -- does nothing.

Parameters:
event
path
manager

removeNotify

public void removeNotify()
Overrides JComponent.removeNotify to unregister this menu bar with the current keyboard manager.

Overrides:
removeNotify in class JComponent

setBorderPainted

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.

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

setMargin

public void setMargin(Insets m)
Sets the margin between the menubar's border and its menus. Setting to null will cause the menubar to use the default margins.

Parameters:
m - an Insets object containing the margin values

setSelected

public void setSelected(Component sel)
Sets the currently selected component, producing a a change to the selection model.

Parameters:
sel - the Component to select

setSelectionModel

public void setSelectionModel(SingleSelectionModel model)
Sets the model object to handle single selections.

Parameters:
model - the SingleSelectionModel to use

setUI

public void setUI(MenuBarUI ui)
Sets the L&F object that renders this component.

Parameters:
ui - the new MenuBarUI L&F object

updateUI

public void updateUI()
Resets the UI property with a value from the current look and feel.

Overrides:
updateUI in class JComponent


This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.
This page displays the Jadeite version of the documention, which is derived from the offical documentation that contains this copyright notice:
Copyright 2008 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
The official Sun™ documentation can be found here at http://java.sun.com/javase/6/docs/api/.