public class JMenuBar extends JComponentimplements 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.
JMenu,
JPopupMenu,
JMenuItem
| Modifier and Type | Class and Description |
|---|---|
protected class |
JMenuBar
This class implements accessibility support for the
JMenuBar class.
|
JComponent.AccessibleJComponent Container.AccessibleAWTContainer Component.AccessibleAWTComponent , Component.BaselineResizeBehavior , Component.BltBufferStrategy , Component.FlipBufferStrategy listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWaccessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH| Constructor and Description |
|---|
JMenuBar()
Creates a new menu bar.
|
| Modifier and Type | Method and Description |
|---|---|
JMenu |
add(JMenu
Appends the specified menu to the end of the menu bar.
|
void |
addNotify()
Overrides
JComponent.addNotify to register this menu bar with the current keyboard manager.
|
AccessibleContext |
getAccessibleContext()
Gets the AccessibleContext associated with this JMenuBar.
|
Component |
getComponent()
Implemented to be a
MenuElement.
|
Component |
getComponentAtIndex(int i)
Deprecated.
replaced by
getComponent(int i)
|
int |
getComponentIndex(Component
Returns the index of the specified component.
|
JMenu |
getHelpMenu()
Gets the help menu for the menu bar.
|
Insets |
getMargin()
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 |
getMenuCount()
Returns the number of items in the menu bar.
|
SingleSelectionModel |
getSelectionModel()
Returns the model object that handles single selections.
|
MenuElement |
getSubElements()
Implemented to be a
MenuElement -- returns the menus in this menu bar.
|
MenuBarUI |
getUI()
Returns the menubar's current UI.
|
String |
getUIClassID()
Returns the name of the L&F class that renders this component.
|
boolean |
isBorderPainted()
Returns true if the menu bars border should be painted.
|
boolean |
isSelected()
Returns true if the menu bar currently has a component selected.
|
void |
menuSelectionChanged(boolean isIncluded)
Implemented to be a
MenuElement -- does nothing.
|
protected void |
paintBorder(Graphics
Paints the menubar's border if
BorderPainted property is true.
|
protected String |
paramString()
Returns a string representation of this
JMenuBar.
|
protected boolean |
processKeyBinding(KeyStroke
Subclassed to check all the child menus.
|
void |
processKeyEvent(KeyEvent
Implemented to be a
MenuElement -- does nothing.
|
void |
processMouseEvent(MouseEvent
Implemented to be a
MenuElement -- does nothing.
|
void |
removeNotify()
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 |
setHelpMenu(JMenu
Sets the help menu that appears when the user selects the "help" option in the menu bar.
|
void |
setMargin(Insets
Sets the margin between the menubar's border and its menus.
|
void |
setSelected(Component
Sets the currently selected component, producing a a change to the selection model.
|
void |
setSelectionModel(SingleSelectionModel
Sets the model object to handle single selections.
|
void |
setUI(MenuBarUI
Sets the L&F object that renders this component.
|
void |
updateUI()
Resets the UI property with a value from the current look and feel.
|
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, 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, updateadd, 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, validateTreeaction, 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, 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, getPeer, 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, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCyclepublic MenuBarUIgetUI()
setUI(javax.swing.plaf.MenuBarUI)
public void setUI(MenuBarUIui)
ui - the new MenuBarUI L&F object
UIDefaults.getUI(javax.swing.JComponent)
public void updateUI()
updateUI in class
JComponent
JComponent.updateUI()
public StringgetUIClassID()
getUIClassID in class
JComponent
JComponent.getUIClassID() ,
UIDefaults.getUI(javax.swing.JComponent)
public SingleSelectionModelgetSelectionModel()
SingleSelectionModel property
SingleSelectionModel
public void setSelectionModel(SingleSelectionModelmodel)
model - the
SingleSelectionModel to use
SingleSelectionModel
public JMenuadd(JMenu c)
c - the
JMenu component to add
public JMenugetMenu(int index)
index - an integer giving the position in the menu bar, where 0 is the first position
JMenu at that position, or
null if if there is no
JMenu at that position (ie. if it is a
JMenuItem)
public int getMenuCount()
public void setHelpMenu(JMenumenu)
menu - the JMenu that delivers help to the user
public JMenugetHelpMenu()
JMenu that delivers help to the user
@Deprecated public ComponentgetComponentAtIndex(int i)
getComponent(int i)
i - an integer specifying the position, where 0 is first
Component at the position, or
null for an invalid index
public int getComponentIndex(Componentc)
c - the
Component to find
public void setSelected(Componentsel)
sel - the
Component to select
public boolean isSelected()
public boolean isBorderPainted()
public void setBorderPainted(boolean b)
b - if true and border property is not
null, the border is painted.
isBorderPainted()
protected void paintBorder(Graphicsg)
BorderPainted property is true.
paintBorder in class
JComponent
g - the
Graphics context to use for painting
JComponent.paint(java.awt.Graphics) ,
JComponent.setBorder(javax.swing.border.Border)
public void setMargin(Insetsm)
null will cause the menubar to use the default margins.
m - an Insets object containing the margin values
Insets
public InsetsgetMargin()
Insets object containing the margin values
Insets
public void processMouseEvent(MouseEventevent, MenuElement [] path, MenuSelectionManager manager)
MenuElement -- does nothing.
processMouseEvent in interface
MenuElement
getSubElements()
public void processKeyEvent(KeyEvente, MenuElement [] path, MenuSelectionManager manager)
MenuElement -- does nothing.
processKeyEvent in interface
MenuElement
getSubElements()
public void menuSelectionChanged(boolean isIncluded)
MenuElement -- does nothing.
menuSelectionChanged in interface
MenuElement
getSubElements()
public MenuElement[] getSubElements()
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.
getSubElements in interface
MenuElement
public ComponentgetComponent()
MenuElement. Returns this object.
getComponent in interface
MenuElement
Component (this)
getSubElements()
protected StringparamString()
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.
paramString in class
JComponent
JMenuBar
public AccessibleContextgetAccessibleContext()
getAccessibleContext in interface
Accessible
getAccessibleContext in class
Component
protected boolean processKeyBinding(KeyStrokeks, KeyEvent e, int condition, boolean pressed)
processKeyBinding in class
JComponent
ks - the
KeyStroke queried
e - the
KeyEvent
condition - one of the following values:
pressed - true if the key is pressed
public void addNotify()
JComponent.addNotify to register this menu bar with the current keyboard manager.
public void removeNotify()
JComponent.removeNotify to unregister this menu bar with the current keyboard manager.