javax.swing

Class JMenuBar

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.
Nested Class Summary
protected classJMenuBar.AccessibleJMenuBar
Provides accessibility support for JMenuBar.
Constructor Summary
JMenuBar()
Creates a new JMenuBar object.
Method Summary
JMenuadd(JMenu c)
Adds menu to the menu bar
voidaddNotify()
This method overrides addNotify() in the Container to register this menu bar with the current keyboard manager.
AccessibleContextgetAccessibleContext()
ComponentgetComponent()
Returns reference to this menu bar
ComponentgetComponentAtIndex(int i)
Returns component at the specified index.
intgetComponentIndex(Component c)
Returns index of the specified component
JMenugetHelpMenu()
This method is not implemented and will throw an {@link Error} if called.
InsetsgetMargin()
Returns the margin between the menu bar's border and its menus.
JMenugetMenu(int index)
Return menu at the specified index.
intgetMenuCount()
Returns number of menu's in this menu bar
SingleSelectionModelgetSelectionModel()
Returns selection model for this menu bar.
MenuElement[]getSubElements()
Method of MenuElement interface.
MenuBarUIgetUI()
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.
StringgetUIClassID()
This method returns a name to identify which look and feel class will be the UI delegate for the menu bar.
booleanisBorderPainted()
Returns true if menu bar paints its border and false otherwise
booleanisSelected()
Returns true if some menu in menu bar is selected.
voidmenuSelectionChanged(boolean isIncluded)
This method does nothing by default.
protected voidpaintBorder(Graphics g)
Paints border of the menu bar, if its borderPainted property is set to true.
protected StringparamString()
A string that describes this JMenuBar.
protected booleanprocessKeyBinding(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.
voidprocessKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager)
Process key events forwarded from MenuSelectionManager.
voidprocessMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager)
Process mouse events forwarded from MenuSelectionManager.
voidremoveNotify()
This method overrides removeNotify() in the Container to unregister this menu bar from the current keyboard manager.
voidsetBorderPainted(boolean b)
Sets painting status of the border.
voidsetHelpMenu(JMenu menu)
Sets help menu for this menu bar
voidsetMargin(Insets m)
Sets the margin between the menu bar's border and its menus (this is a bound property with the name 'margin').
voidsetSelected(Component sel)
Changes menu bar's selection to the specified menu.
voidsetSelectionModel(SingleSelectionModel model)
Sets menuBar's selection model to the one specified
voidsetUI(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.
voidupdateUI()
Set the "UI" property to a class constructed, via the {@link UIManager}, from the current look and feel.

Constructor Detail

JMenuBar

public JMenuBar()
Creates a new JMenuBar object.

Method Detail

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.

getAccessibleContext

public AccessibleContext getAccessibleContext()

getComponent

public Component getComponent()
Returns reference to this menu bar

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 {@link 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).

See Also: setMargin

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.

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.

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.

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.

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.

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.

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.

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.

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.

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

UNKNOWN: The specification states that this method is not yet implemented and should throw an exception.

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 {@link UIManager}, from the current look and feel.