com.google.gwt.user.client.ui
Class MenuBar

java.lang.Object
  extended by com.google.gwt.user.client.ui.UIObject
      extended by com.google.gwt.user.client.ui.Widget
          extended by com.google.gwt.user.client.ui.MenuBar
All Implemented Interfaces:
EventListener, PopupListener

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

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);
  }
}


Constructor Summary
MenuBar()
          Creates an empty horizontal menu bar.
MenuBar(boolean vertical)
          Creates an empty menu bar.
 
Method Summary
 void addItem(MenuItem item)
          Adds a menu item to the bar.
 MenuItem addItem(java.lang.String text, boolean asHTML, Command cmd)
          Adds a menu item to the bar, that will fire the given command when it is selected.
 MenuItem addItem(java.lang.String text, boolean asHTML, MenuBar popup)
          Adds a menu item to the bar, that will open the specified menu when it is selected.
 MenuItem addItem(java.lang.String text, Command cmd)
          Adds a menu item to the bar, that will fire the given command when it is selected.
 MenuItem addItem(java.lang.String text, MenuBar popup)
          Adds a menu item to the bar, that will open the specified menu when it is selected.
 void clearItems()
          Removes all menu items from this menu bar.
 boolean getAutoOpen()
          Gets whether this menu bar's child menus will open when the mouse is moved over it.
protected  java.util.List getItems()
          Returns a list containing the MenuItem objects in the menu bar.
protected  MenuItem getSelectedItem()
          Returns the MenuItem that is currently selected (highlighted) by the user.
 void onBrowserEvent(Event event)
          Fired whenever a browser event is received.
protected  void onDetach()
          This method is called when a widget is detached from the browser's document.
 void onPopupClosed(PopupPanel sender, boolean autoClosed)
          Fired when the popup is closed.
 void removeItem(MenuItem item)
          Removes the specified menu item from the bar.
 void setAutoOpen(boolean autoOpen)
          Sets whether this menu bar's child menus will open when the mouse is moved over it.
 
Methods inherited from class com.google.gwt.user.client.ui.Widget
doAttachChildren, doDetachChildren, getParent, isAttached, onAttach, onLoad, onUnload, removeFromParent, setElement
 
Methods inherited from class com.google.gwt.user.client.ui.UIObject
addStyleDependentName, addStyleName, getAbsoluteLeft, getAbsoluteTop, getElement, getOffsetHeight, getOffsetWidth, getStyleElement, getStyleName, getStyleName, getStylePrimaryName, getStylePrimaryName, getTitle, isVisible, isVisible, removeStyleDependentName, removeStyleName, setHeight, setPixelSize, setSize, setStyleName, setStyleName, setStyleName, setStylePrimaryName, setStylePrimaryName, setTitle, setVisible, setVisible, setWidth, sinkEvents, toString, unsinkEvents
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

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(java.lang.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
Returns:
the MenuItem object created

addItem

public MenuItem addItem(java.lang.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
Returns:
the MenuItem object created

addItem

public MenuItem addItem(java.lang.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
Returns:
the MenuItem object created

addItem

public MenuItem addItem(java.lang.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
Returns:
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.

Returns:
true if child menus will auto-open

onBrowserEvent

public void onBrowserEvent(Event event)
Description copied from interface: EventListener
Fired whenever a browser event is received.

Specified by:
onBrowserEvent in interface EventListener
Overrides:
onBrowserEvent in class Widget
Parameters:
event - the event received

onPopupClosed

public void onPopupClosed(PopupPanel sender,
                          boolean autoClosed)
Description copied from interface: PopupListener
Fired when the popup is closed.

Specified by:
onPopupClosed in interface PopupListener
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

getItems

protected java.util.List getItems()
Returns a list containing the MenuItem objects in the menu bar. If there are no items in the menu bar, then an empty List object will be returned.

Returns:
a list containing the MenuItem objects in the menu bar

getSelectedItem

protected MenuItem getSelectedItem()
Returns the MenuItem that is currently selected (highlighted) by the user. If none of the items in the menu are currently selected, then null will be returned.

Returns:
the MenuItem that is currently selected, or null if no items are currently selected

onDetach

protected void onDetach()
Description copied from class: Widget
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 Widget.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 memory leaks due to circular references between DOM Elements and JavaScript objects.

Overrides:
onDetach in class Widget