javax.swing
Class JMenuBar

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--javax.swing.JMenuBar
All Implemented Interfaces:
Accessible, java.awt.image.ImageObserver, MenuContainer, MenuElement, Serializable

public class JMenuBar
extends JComponent
implements Accessible, MenuElement

Safe:

See Also:
Serialized Form

Field Summary
private static boolean DEBUG
           
private  Insets margin
           
private  boolean paintBorder
           
private  SingleSelectionModel selectionModel
           
private static boolean TRACE
           
private static String uiClassID
           
private static boolean VERBOSE
           
 
Fields inherited from class javax.swing.JComponent
_bounds, accessibleContext, listenerList, paintingChild, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Container
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Constructor Summary
JMenuBar()
          Enabled: Creates a new menu bar.
 
Method Summary
 JMenu add(JMenu c)
          Enabled: Appends the specified menu to the end of the menu bar.
 void addNotify()
          Suppressed: Overrides JComponent.addNotify to register this menu bar with the current keyboard manager.
 AccessibleContext getAccessibleContext()
          Suppressed: Gets the AccessibleContext associated with this JMenuBar.
 Component getComponent()
          Enabled: Implemented to be a MenuElement.
 Component getComponentAtIndex(int i)
          Deprecated. replaced by getComponent(int i)
 int getComponentIndex(Component c)
          Enabled: Returns the index of the specified component.
 JMenu getHelpMenu()
          Enabled: Gets the help menu for the menu bar.
 Insets getMargin()
          Enabled: Returns the margin between the menubar's border and its menus.
 JMenu getMenu(int index)
          Enabled: Returns the menu at the specified position in the menu bar.
 int getMenuCount()
          Enabled: Returns the number of items in the menu bar.
 SingleSelectionModel getSelectionModel()
          Enabled: Returns the model object that handles single selections.
 MenuElement[] getSubElements()
          Enabled: Implemented to be a MenuElement -- returns the menus in this menu bar.
 javax.swing.plaf.MenuBarUI getUI()
          Suppressed: Returns the menubar's current UI.
 String getUIClassID()
          Suppressed: Returns the name of the L&F class that renders this component.
 boolean isBorderPainted()
          Enabled: Returns true if the menu bars border should be painted.
 boolean isSelected()
          Enabled: Returns true if the menu bar currently has a component selected.
 void menuSelectionChanged(boolean isIncluded)
          Suppressed: 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.
(package private) static boolean processBindingForKeyStrokeRecursive(MenuElement elem, KeyStroke ks, KeyEvent e, int condition, boolean pressed)
           
protected  boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
          Subclassed to check all the child menus.
 void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
          Suppressed: Implemented to be a MenuElement -- does nothing.
 void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)
          Suppressed: Implemented to be a MenuElement -- does nothing.
private  void readObject(ObjectInputStream s)
          See JComponent.readObject() for information about serialization in Swing.
 void removeNotify()
          Suppressed: Overrides JComponent.removeNotify to unregister this menu bar with the current keyboard manager.
 void setBorderPainted(boolean b)
          Enabled: Sets whether the border should be painted.
 void setHelpMenu(JMenu menu)
          Enabled: Sets the help menu that appears when the user selects the "help" option in the menu bar.
 void setMargin(Insets m)
          Enabled: Sets the margin between the menubar's border and its menus.
 void setSelected(Component sel)
          Enabled: Sets the currently selected component, producing a a change to the selection model.
 void setSelectionModel(SingleSelectionModel model)
          Enabled: Sets the model object to handle single selections.
 void setUI(javax.swing.plaf.MenuBarUI ui)
          Suppressed: Sets the L&F object that renders this component.
 void updateUI()
          Suppressed: Resets the UI property with a value from the current look and feel.
private  void writeObject(ObjectOutputStream s)
          Before writing a JComponent to an ObjectOutputStream we temporarily uninstall its UI.
 
Methods inherited from class javax.swing.JComponent
_paintImmediately, addAncestorListener, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, alwaysOnTop, checkIfChildObscuredBySibling, componentInputMapChanged, computeVisibleRect, computeVisibleRect, compWriteObjectNotify, contains, createToolTip, disable, enable, enableSerialization, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getCreatedDoubleBuffer, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getManagingFocusBackwardTraversalKeys, getManagingFocusForwardTraversalKeys, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getWriteObjCounter, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBindings, processKeyBindingsForAllComponents, processKeyEvent, processMouseMotionEvent, putClientProperty, rectangleIsObscured, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setCreatedDoubleBuffer, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPaintingChild, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, setWriteObjCounter, shouldDebugGraphics, superProcessMouseMotionEvent, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, get, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, iterate, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, size, transferFocusBackward, 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, 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, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, 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, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, toString, transferFocus, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

uiClassID

private static final String uiClassID
See Also:
getUIClassID(), readObject(java.io.ObjectInputStream)

selectionModel

private transient SingleSelectionModel selectionModel

paintBorder

private boolean paintBorder

margin

private Insets margin

TRACE

private static final boolean TRACE

VERBOSE

private static final boolean VERBOSE

DEBUG

private static final boolean DEBUG
Constructor Detail

JMenuBar

public JMenuBar()
Enabled: Creates a new menu bar.

Method Detail

getUI

public javax.swing.plaf.MenuBarUI getUI()
Suppressed: Returns the menubar's current UI.

See Also:
setUI(javax.swing.plaf.MenuBarUI)

setUI

public void setUI(javax.swing.plaf.MenuBarUI ui)
Suppressed: 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()
Suppressed: Resets the UI property with a value from the current look and feel.

Overrides:
updateUI in class JComponent
See Also:
JComponent.updateUI()

getUIClassID

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

Overrides:
getUIClassID in class JComponent
Returns:
the string "MenuBarUI"
See Also:
JComponent.getUIClassID(), UIDefaults.getUI(javax.swing.JComponent)

getSelectionModel

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

Returns:
the SingleSelectionModel property
See Also:
SingleSelectionModel

setSelectionModel

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

Parameters:
model - the SingleSelectionModel to use
See Also:
SingleSelectionModel

add

public JMenu add(JMenu c)
Enabled: 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)
Enabled: 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()
Enabled: Returns the number of items in the menu bar.

Returns:
the number of items in the menu bar

setHelpMenu

public void setHelpMenu(JMenu menu)
Enabled: 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()
Enabled: 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

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

Enabled: 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)
Enabled: 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)
Enabled: Sets the currently selected component, producing a a change to the selection model.

Parameters:
sel - the Component to select

isSelected

public boolean isSelected()
Enabled: 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()
Enabled: 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)
Enabled: Sets whether the border should be painted.

Parameters:
b - if true and border property is not null, the border is painted.
See Also:
isBorderPainted()

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
See Also:
JComponent.paint(java.awt.Graphics), JComponent.setBorder(javax.swing.border.Border)

setMargin

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

See Also:
Insets

getMargin

public Insets getMargin()
Enabled: 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:
Insets

processMouseEvent

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

Specified by:
processMouseEvent in interface MenuElement
See Also:
getSubElements()

processKeyEvent

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

Specified by:
processKeyEvent in interface MenuElement
See Also:
getSubElements()

menuSelectionChanged

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

Specified by:
menuSelectionChanged in interface MenuElement
See Also:
getSubElements()

getSubElements

public MenuElement[] getSubElements()
Enabled: 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 in interface MenuElement
Returns:
an array of menu items in the menu bar.

getComponent

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

Specified by:
getComponent in interface MenuElement
Returns:
the current Component (this)
See Also:
getSubElements()

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

getAccessibleContext

public AccessibleContext getAccessibleContext()
Suppressed: 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 in interface Accessible
Overrides:
getAccessibleContext in class JComponent
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 in class JComponent
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

processBindingForKeyStrokeRecursive

static boolean processBindingForKeyStrokeRecursive(MenuElement elem,
                                                   KeyStroke ks,
                                                   KeyEvent e,
                                                   int condition,
                                                   boolean pressed)

addNotify

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

Overrides:
addNotify in class JComponent
See Also:
JComponent.registerKeyboardAction(java.awt.event.ActionListener, java.lang.String, javax.swing.KeyStroke, int)

removeNotify

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

Overrides:
removeNotify in class JComponent
See Also:
JComponent.registerKeyboardAction(java.awt.event.ActionListener, java.lang.String, javax.swing.KeyStroke, int)

writeObject

private void writeObject(ObjectOutputStream s)
                  throws IOException
Description copied from class: JComponent
Before writing a JComponent to an ObjectOutputStream we temporarily uninstall its UI. This is tricky to do because we want to uninstall the UI before any of the JComponent's children (or its LayoutManager etc.) are written, and we don't want to restore the UI until the most derived JComponent subclass has been been stored.

Parameters:
s - the ObjectOutputStream in which to write
IOException

readObject

private void readObject(ObjectInputStream s)
                 throws IOException,
                        ClassNotFoundException
See JComponent.readObject() for information about serialization in Swing.

Parameters:
s - the ObjectInputStream from which to read
IOException
ClassNotFoundException
See Also:
ReadObjectCallback


comments?