JMenuBar (Java 2 Platform SE 5.0) (original) (raw)
javax.swing
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
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 seeHow to Use Menus, a section in The Java Tutorial.
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 XMLEncoder.
See Also:
Nested Class Summary | |
---|---|
protected class | JMenuBar.AccessibleJMenuBar 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.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 menu bar. |
Method Summary | |
---|---|
JMenu | add(JMenu c) 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 c) 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 g) Paints the menubar's border if BorderPainted property is true. |
protected String | paramString() Returns a string representation of this JMenuBar. |
protected boolean | [processKeyBinding](../../javax/swing/JMenuBar.html#processKeyBinding%28javax.swing.KeyStroke, java.awt.event.KeyEvent, int, boolean%29)(KeyStroke ks,KeyEvent e, int condition, boolean pressed) Subclassed to check all the child menus. |
void | [processKeyEvent](../../javax/swing/JMenuBar.html#processKeyEvent%28java.awt.event.KeyEvent, javax.swing.MenuElement[], javax.swing.MenuSelectionManager%29)(KeyEvent e,MenuElement[] path,MenuSelectionManager manager) Implemented to be a MenuElement -- does nothing. |
void | [processMouseEvent](../../javax/swing/JMenuBar.html#processMouseEvent%28java.awt.event.MouseEvent, javax.swing.MenuElement[], javax.swing.MenuSelectionManager%29)(MouseEvent event,MenuElement[] path,MenuSelectionManager manager) 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 menu) Sets the help menu that appears when the user selects the "help" option in the menu bar. |
void | setMargin(Insets m) Sets the margin between the menubar's border and its menus. |
void | setSelected(Component sel) Sets the currently selected component, producing a a change to the selection model. |
void | setSelectionModel(SingleSelectionModel model) Sets the model object to handle single selections. |
void | setUI(MenuBarUI ui) Sets the L&F object that renders this component. |
void | updateUI() Resets the UI property with a value from the current look and feel. |
Methods inherited from class java.awt.Container |
---|
add, [add](../../java/awt/Container.html#add%28java.awt.Component, int%29), [add](../../java/awt/Container.html#add%28java.awt.Component, java.lang.Object%29), [add](../../java/awt/Container.html#add%28java.awt.Component, java.lang.Object, int%29), [add](../../java/awt/Container.html#add%28java.lang.String, java.awt.Component%29), addContainerListener, [addImpl](../../java/awt/Container.html#addImpl%28java.awt.Component, java.lang.Object, int%29), addPropertyChangeListener, [addPropertyChangeListener](../../java/awt/Container.html#addPropertyChangeListener%28java.lang.String, java.beans.PropertyChangeListener%29), applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, [findComponentAt](../../java/awt/Container.html#findComponentAt%28int, int%29), findComponentAt, getComponent, [getComponentAt](../../java/awt/Container.html#getComponentAt%28int, int%29), getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, [list](../../java/awt/Container.html#list%28java.io.PrintStream, int%29), [list](../../java/awt/Container.html#list%28java.io.PrintWriter, int%29), [locate](../../java/awt/Container.html#locate%28int, int%29), minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, [setComponentZOrder](../../java/awt/Container.html#setComponentZOrder%28java.awt.Component, int%29), setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree |
Methods inherited from class java.awt.Component |
---|
[action](../../java/awt/Component.html#action%28java.awt.Event, java.lang.Object%29), add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, [checkImage](../../java/awt/Component.html#checkImage%28java.awt.Image, java.awt.image.ImageObserver%29), [checkImage](../../java/awt/Component.html#checkImage%28java.awt.Image, int, int, java.awt.image.ImageObserver%29), [coalesceEvents](../../java/awt/Component.html#coalesceEvents%28java.awt.AWTEvent, java.awt.AWTEvent%29), contains, createImage, [createImage](../../java/awt/Component.html#createImage%28int, int%29), [createVolatileImage](../../java/awt/Component.html#createVolatileImage%28int, int%29), [createVolatileImage](../../java/awt/Component.html#createVolatileImage%28int, int, java.awt.ImageCapabilities%29), disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, byte, byte%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, double, double%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, float, float%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, long, long%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, java.lang.Object, java.lang.Object%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, short, short%29), 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](../../java/awt/Component.html#gotFocus%28java.awt.Event, java.lang.Object%29), handleEvent, hasFocus, hide, [imageUpdate](../../java/awt/Component.html#imageUpdate%28java.awt.Image, int, int, int, int, int%29), [inside](../../java/awt/Component.html#inside%28int, int%29), isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, [keyDown](../../java/awt/Component.html#keyDown%28java.awt.Event, int%29), [keyUp](../../java/awt/Component.html#keyUp%28java.awt.Event, int%29), list, list, list, location, [lostFocus](../../java/awt/Component.html#lostFocus%28java.awt.Event, java.lang.Object%29), [mouseDown](../../java/awt/Component.html#mouseDown%28java.awt.Event, int, int%29), [mouseDrag](../../java/awt/Component.html#mouseDrag%28java.awt.Event, int, int%29), [mouseEnter](../../java/awt/Component.html#mouseEnter%28java.awt.Event, int, int%29), [mouseExit](../../java/awt/Component.html#mouseExit%28java.awt.Event, int, int%29), [mouseMove](../../java/awt/Component.html#mouseMove%28java.awt.Event, int, int%29), [mouseUp](../../java/awt/Component.html#mouseUp%28java.awt.Event, int, int%29), [move](../../java/awt/Component.html#move%28int, int%29), nextFocus, paintAll, postEvent, [prepareImage](../../java/awt/Component.html#prepareImage%28java.awt.Image, java.awt.image.ImageObserver%29), [prepareImage](../../java/awt/Component.html#prepareImage%28java.awt.Image, int, int, java.awt.image.ImageObserver%29), processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, [removePropertyChangeListener](../../java/awt/Component.html#removePropertyChangeListener%28java.lang.String, java.beans.PropertyChangeListener%29), repaint, [repaint](../../java/awt/Component.html#repaint%28int, int, int, int%29), repaint, resize, [resize](../../java/awt/Component.html#resize%28int, int%29), [setBounds](../../java/awt/Component.html#setBounds%28int, int, int, int%29), setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, [setLocation](../../java/awt/Component.html#setLocation%28int, int%29), setLocation, setName, setSize, [setSize](../../java/awt/Component.html#setSize%28int, int%29), show, show, size, toString, transferFocus, transferFocusUpCycle |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, [wait](../../java/lang/Object.html#wait%28long, int%29) |
Constructor Detail |
---|
JMenuBar
public JMenuBar()
Creates a new menu bar.
Method Detail |
---|
getUI
public MenuBarUI getUI()
Returns the menubar's current UI.
See Also:
setUI(javax.swing.plaf.MenuBarUI)
setUI
public void setUI(MenuBarUI ui)
Sets the L&F object that renders this component.
Parameters:
ui
- the new MenuBarUI L&F object
See Also:
UIDefaults.getUI(javax.swing.JComponent)
updateUI
public void updateUI()
Resets the UI property with a value from the current look and feel.
Overrides:
[updateUI](../../javax/swing/JComponent.html#updateUI%28%29)
in class [JComponent](../../javax/swing/JComponent.html "class in javax.swing")
See Also:
getUIClassID
public String getUIClassID()
Returns the name of the L&F class that renders this component.
Overrides:
[getUIClassID](../../javax/swing/JComponent.html#getUIClassID%28%29)
in class [JComponent](../../javax/swing/JComponent.html "class in javax.swing")
Returns:
the string "MenuBarUI"
See Also:
JComponent.getUIClassID(), UIDefaults.getUI(javax.swing.JComponent)
getSelectionModel
public SingleSelectionModel getSelectionModel()
Returns the model object that handles single selections.
Returns:
the SingleSelectionModel
property
See Also:
setSelectionModel
public void setSelectionModel(SingleSelectionModel model)
Sets the model object to handle single selections.
Parameters:
model
- the SingleSelectionModel
to use
See Also:
add
Appends the specified menu to the end of the menu bar.
Parameters:
c
- the JMenu
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:
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
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:
the JMenu
that delivers help to the user
getComponentAtIndex
@Deprecated public Component getComponentAtIndex(int i)
Deprecated. replaced by getComponent(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
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
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
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
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.
See Also:
paintBorder
protected void paintBorder(Graphics g)
Paints the menubar's border if BorderPainted
property is true.
Overrides:
[paintBorder](../../javax/swing/JComponent.html#paintBorder%28java.awt.Graphics%29)
in class [JComponent](../../javax/swing/JComponent.html "class in javax.swing")
Parameters:
g
- the Graphics
context to use for painting
See Also:
JComponent.paint(java.awt.Graphics), JComponent.setBorder(javax.swing.border.Border)
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
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:
an Insets
object containing the margin values
See Also:
processMouseEvent
public void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)
Implemented to be a MenuElement
-- does nothing.
Specified by:
[processMouseEvent](../../javax/swing/MenuElement.html#processMouseEvent%28java.awt.event.MouseEvent, javax.swing.MenuElement[], javax.swing.MenuSelectionManager%29)
in interface [MenuElement](../../javax/swing/MenuElement.html "interface in javax.swing")
See Also:
processKeyEvent
public void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
Implemented to be a MenuElement
-- does nothing.
Specified by:
[processKeyEvent](../../javax/swing/MenuElement.html#processKeyEvent%28java.awt.event.KeyEvent, javax.swing.MenuElement[], javax.swing.MenuSelectionManager%29)
in interface [MenuElement](../../javax/swing/MenuElement.html "interface in javax.swing")
See Also:
menuSelectionChanged
public void menuSelectionChanged(boolean isIncluded)
Implemented to be a MenuElemen`t -- does nothing.`
``` `` Specified by:
[menuSelectionChanged](../../javax/swing/MenuElement.html#menuSelectionChanged%28boolean%29)
in interface [MenuElement](../../javax/swing/MenuElement.html "interface in javax.swing")
`` ```
``
`` `See Also:
` ``
``` `` ---
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.
Specified by:
[getSubElements](../../javax/swing/MenuElement.html#getSubElements%28%29)
in interface [MenuElement](../../javax/swing/MenuElement.html "interface in javax.swing")
Returns:
an array of menu items in the menu bar.
getComponent
public Component getComponent()
Implemented to be a MenuElement
. Returns this object.
Specified by:
[getComponent](../../javax/swing/MenuElement.html#getComponent%28%29)
in interface [MenuElement](../../javax/swing/MenuElement.html "interface in javax.swing")
Returns:
the current Component
(this)
See Also:
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](../../javax/swing/JComponent.html#paramString%28%29)
in class [JComponent](../../javax/swing/JComponent.html "class in javax.swing")
Returns:
a string representation of this JMenuBar
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.
Specified by:
[getAccessibleContext](../../javax/accessibility/Accessible.html#getAccessibleContext%28%29)
in interface [Accessible](../../javax/accessibility/Accessible.html "interface in javax.accessibility")
Overrides:
[getAccessibleContext](../../javax/swing/JComponent.html#getAccessibleContext%28%29)
in class [JComponent](../../javax/swing/JComponent.html "class in javax.swing")
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](../../javax/swing/JComponent.html#processKeyBinding%28javax.swing.KeyStroke, java.awt.event.KeyEvent, int, boolean%29)
in class [JComponent](../../javax/swing/JComponent.html "class in javax.swing")
Parameters:
ks
- the KeyStroke
queried
e
- the KeyEvent
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
addNotify
public void addNotify()
Overrides JComponent.addNotify
to register this menu bar with the current keyboard manager.
Overrides:
[addNotify](../../javax/swing/JComponent.html#addNotify%28%29)
in class [JComponent](../../javax/swing/JComponent.html "class in javax.swing")
See Also:
[JComponent.registerKeyboardAction(java.awt.event.ActionListener, java.lang.String, javax.swing.KeyStroke, int)](../../javax/swing/JComponent.html#registerKeyboardAction%28java.awt.event.ActionListener, java.lang.String, javax.swing.KeyStroke, int%29)
removeNotify
public void removeNotify()
Overrides JComponent.removeNotify
to unregister this menu bar with the current keyboard manager.
Overrides:
[removeNotify](../../javax/swing/JComponent.html#removeNotify%28%29)
in class [JComponent](../../javax/swing/JComponent.html "class in javax.swing")
See Also:
[JComponent.registerKeyboardAction(java.awt.event.ActionListener, java.lang.String, javax.swing.KeyStroke, int)](../../javax/swing/JComponent.html#registerKeyboardAction%28java.awt.event.ActionListener, java.lang.String, javax.swing.KeyStroke, int%29)
Overview Package Class Use Tree Deprecated Index Help | JavaTM 2 PlatformStandard Ed. 5.0 | |||
---|---|---|---|---|
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 2004, 2010 Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
`` ```