Search code examples
gwtvaadinvaadin7

How to set a title (not a caption)for MenuBar in Vaadin


I'd like to have a title for my MenuBar, but I'm not finding any methods in the Vaadin API for that. Just a small text to appear on the button which triggers the dropdown (like the user's name), instead of the triangle it shows now. Thanks!

MenuBar menu = new MenuBar();
menu.setWidth("70px");

MenuItem home = menu.addItem("Home", FontAwesome.HOME, new Command() {
         public void menuSelected(MenuItem selectedItem) {doSomething();}});
MenuItem settings = menu.addItem("Settings", FontAwesome.COGS, new Command() {
         public void menuSelected(MenuItem selectedItem) {doSomething();}});
MenuItem logout = menu.addItem("Sign out", FontAwesome.SIGN_OUT, new Command() {
         public void menuSelected(MenuItem selectedItem) {doSomething();}});

Solution

  • If you remove the setWidth you will notice that all three menus (Home, Settings, Sign out) are horizontally aligned which is, I believe, not what you want. What you want to do is more something like this:

    MenuBar menu = new MenuBar();
    MenuItem rootItem = menu.addItem("User name", null);
    rootItem.addItem("Home", FontAwesome.HOME, this::doSomething);
    rootItem.addItem("Settings", FontAwesome.COGS, this::doSomething);
    rootItem.addItem("Sign out", FontAwesome.SIGN_OUT, this::doSomething);