Google Web Toolkit (Beta)
Search this site:

# Class MenuBar

public class MenuBar
extends Widget
implements PopupListener
A standard menu bar widget. A menu bar can contain any number of menu items, each of which can either fire a Command or open a cascaded menu bar.

### CSS Style Rules

- .gwt-MenuBar { the menu bar itself }
- .gwt-MenuBar .gwt-MenuItem { menu items }
- .gwt-MenuBar .gwt-MenuItem-selected { selected menu items }

### Example

    public class MenuBarExample implements EntryPoint {

      public void onModuleLoad() {
        // Make a command that we will execute from all leaves.
        Command cmd = new Command() {
          public void execute() {
            Window.alert("You selected a menu item!");
          }
        };

        // Make some sub-menus that we will cascade from the top menu.
        MenuBar fooMenu = new MenuBar(true);
        fooMenu.addItem("the", cmd);
        fooMenu.addItem("foo", cmd);
        fooMenu.addItem("menu", cmd);

        MenuBar barMenu = new MenuBar(true);
        barMenu.addItem("the", cmd);
        barMenu.addItem("bar", cmd);
        barMenu.addItem("menu", cmd);

        MenuBar bazMenu = new MenuBar(true);
        bazMenu.addItem("the", cmd);
        bazMenu.addItem("baz", cmd);
        bazMenu.addItem("menu", cmd);

        // Make a new menu bar, adding a few cascading menus to it.
        MenuBar menu = new MenuBar();
        menu.addItem("foo", fooMenu);
        menu.addItem("bar", barMenu);
        menu.addItem("baz", bazMenu);

        // Add it to the root panel.
        RootPanel.get().add(menu);
      }
    }

## Constructors

MenuBar() | Creates an empty horizontal menu bar.
MenuBar(boolean) | Creates an empty menu bar.

## Methods

addItem(MenuItem) | Adds a menu item to the bar.
addItem(String, boolean, Command) | Adds a menu item to the bar, that will fire the given command when it is selected.
addItem(String, boolean, MenuBar) | Adds a menu item to the bar, that will open the specified menu when it is selected.
addItem(String, Command) | Adds a menu item to the bar, that will fire the given command when it is selected.
addItem(String, MenuBar) | Adds a menu item to the bar, that will open the specified menu when it is selected.
clearItems() | Removes all menu items from this menu bar.
getAutoOpen() | Gets whether this menu bar's child menus will open when the mouse is moved over it.
onBrowserEvent(Event) |
onDetach() | This method is called when a widget is detached from the browser's document.
onPopupClosed(PopupPanel, boolean) | Fired when the popup is closed.
removeItem(MenuItem) | Removes the specified menu item from the bar.
setAutoOpen(boolean) | Sets whether this menu bar's child menus will open when the mouse is moved over it.

## Constructor Detail

### MenuBar

public MenuBar()
Creates an empty horizontal menu bar.

### MenuBar

public MenuBar(boolean vertical)
Creates an empty menu bar.

#### Parameters

vertical
  `true` to orient the menu bar vertically

## Method Detail

### addItem

public void addItem(MenuItem item)
Adds a menu item to the bar.

#### Parameters

item
  the item to be added

### addItem

public MenuItem addItem(String text, boolean asHTML, Command cmd)
Adds a menu item to the bar, that will fire the given command when it is selected.

#### Parameters

text
  the item's text
asHTML
  `true` to treat the specified text as html
cmd
  the command to be fired

#### Return Value

the MenuItem object created

### addItem

public MenuItem addItem(String text, boolean asHTML, MenuBar popup)
Adds a menu item to the bar, that will open the specified menu when it is selected.

#### Parameters

text
  the item's text
asHTML
  `true` to treat the specified text as html
popup
  the menu to be cascaded from it

#### Return Value

the MenuItem object created

### addItem

public MenuItem addItem(String text, Command cmd)
Adds a menu item to the bar, that will fire the given command when it is selected.

#### Parameters

text
  the item's text
cmd
  the command to be fired

#### Return Value

the MenuItem object created

### addItem

public MenuItem addItem(String text, MenuBar popup)
Adds a menu item to the bar, that will open the specified menu when it is selected.

#### Parameters

text
  the item's text
popup
  the menu to be cascaded from it

#### Return Value

the MenuItem object created

### clearItems

public void clearItems()
Removes all menu items from this menu bar.

### getAutoOpen

public boolean getAutoOpen()
Gets whether this menu bar's child menus will open when the mouse is moved over it.

#### Return Value

`true` if child menus will auto-open

### onBrowserEvent

public void onBrowserEvent(Event event)

#### Parameters

event

### onDetach

protected void onDetach()
This method is called when a widget is detached from the browser's document. To receive notification before a Widget is removed from the document, override the onUnload method.

Subclasses that override this method must call `super.onDetach()` to ensure that the Widget has been detached from the underlying Element. Failure to do so will result in application memeroy leaks due to circular references between DOM Elements and JavaScript objects.

### onPopupClosed

public void onPopupClosed(PopupPanel sender, boolean autoClosed)
Fired when the popup is closed.

#### Parameters

sender
  popup being closed.
autoClosed
  `true` if the popup was automatically closed; `false` if it was closed programmatically.

### removeItem

public void removeItem(MenuItem item)
Removes the specified menu item from the bar.

#### Parameters

item
  the item to be removed

### setAutoOpen

public void setAutoOpen(boolean autoOpen)
Sets whether this menu bar's child menus will open when the mouse is moved over it.

#### Parameters

autoOpen
  `true` to cause child menus to auto-open