Table of contents
1.
Introduction
2.
Menu Bar
3.
Vaadin-Menu bar Features
3.1.
Overflow
3.2.
Icons
3.3.
Disabled Items
3.4.
Checkable Menu Items
3.5.
Dividers
3.6.
Open on Hover
4.
Keyboard Usage
5.
Frequently Asked Questions
5.1.
What does a menu bar do?
5.2.
Who is Vaadin for?
5.3.
Is vaadin free?
6.
Conclusion
Last Updated: Mar 27, 2024

Vaadin-Menu Bar

Author Harsh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Vaadin is an open-source web development framework with inbuilt support for both javascript and AJAX. External functionality can also be included into it using the google web toolkit.

In this blog, we will see what is a vaadin-menu bar, and we’ll also see the different types of styling methods available in the vaadin-menu bar.

Vaadin-Menu bar logo

Menu Bar

A horizontal button bar with hierarchical dropdown menus is called a menu bar. Menu items can open a menu, start an activity, or act as a toggle.

There are different styling options available for the Vaadin-Menu bar like default, primary, small, and tertiary. Let’s see all of them.
 

Code:

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.menubar.MenuBar;
import com.vaadin.flow.component.menubar.MenuBarVariant;
import com.vaadin.flow.router.Route;

@Route("menu-bar-styles")
public class MainView extends Div {
    public MainView() {
        MenuBar menuWithDefaultTheme = new MenuBar();
        addItem(menuWithDefaultTheme, "Default Theme");

        MenuBar menuWithTertiaryTheme = new MenuBar();
        menuWithTertiaryTheme.addThemeVariants(MenuBarVariant.LUMO_TERTIARY);
        addItem(menuWithTertiaryTheme, "Tertiary Theme");

        MenuBar menuWithPrimaryTheme = new MenuBar();
        menuWithPrimaryTheme.addThemeVariants(MenuBarVariant.LUMO_PRIMARY);
        addItem(menuWithPrimaryTheme, "Primary Theme");

        MenuBar menuWithSmallTheme = new MenuBar();
        menuWithSmallTheme.addThemeVariants(MenuBarVariant.LUMO_SMALL);
        addItem(menuWithSmallTheme, "Small Theme");

        setInlineBlock(menuWithDefaultTheme);
        setInlineBlock(menuWithTertiaryTheme);
        setInlineBlock(menuWithPrimaryTheme);
        setInlineBlock(menuWithSmallTheme);


        add(menuWithDefaultTheme, menuWithTertiaryTheme, menuWithPrimaryTheme, menuWithSmallTheme);
    }

    private void addItem(MenuBar menuItem, String label) {
        menuItem.addItem(label)
                .getSubMenu()
                .addItem("Item");
    }

    private void setInlineBlock(MenuBar menuBar) {
        menuBar.getStyle().set("display", "inline-block");
    }

}
You can also try this code with Online Java Compiler
Run Code


Output:

 

O/P of the above program

 

difference b/w primary, tertiary and small

Vaadin-Menu bar Features

Vaadin-menu bar provides various features like adding icons, disabling some items, adding checkable items, and many more. Let’s discuss some of them.

Overflow

Items that do not fit within the menu bar's current width are automatically collapsed into an overflow menu.

Code:

import com.vaadin.flow.component.contextmenu.MenuItem;
import com.vaadin.flow.component.contextmenu.SubMenu;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.menubar.MenuBar;
import com.vaadin.flow.component.splitlayout.SplitLayout;
import com.vaadin.flow.router.Route;

@Route("menu-bar-overflow")
public class MenuBarOverflow extends Div {
    public MenuBarOverflow() {
        MenuBar menuBar = new MenuBar();
        addItems(menuBar);
        Div div = new Div();
        div.setText("Move the splitter to see overflow feature");

        SplitLayout splitLayout = new SplitLayout(menuBar, div);

        add(splitLayout);
    }

    private void addItems(MenuBar menuBar) {
        menuBar.addItem("View");
        menuBar.addItem("Edit");

        MenuItem share = menuBar.addItem("Share");
        SubMenu shareSubMenu = share.getSubMenu();
        MenuItem onSocialMedia = shareSubMenu.addItem("On social media");
        SubMenu socialMediaSubMenu = onSocialMedia.getSubMenu();
        socialMediaSubMenu.addItem("Facebook");
        socialMediaSubMenu.addItem("Twitter");
        socialMediaSubMenu.addItem("Instagram");
        shareSubMenu.addItem("By email");
        shareSubMenu.addItem("Get Link");

        MenuItem move = menuBar.addItem("Move");
        SubMenu moveSubMenu = move.getSubMenu();
        moveSubMenu.addItem("To folder");
        moveSubMenu.addItem("To trash");

        menuBar.addItem("Duplicate");
    }

}
You can also try this code with Online Java Compiler
Run Code


Output:
 

O/P of the above program

Icons

Icons may be used in place of or in addition to text for menu items.

Code:

MenuBar menuBar = new MenuBar();
menuBar.addThemeVariants(MenuBarVariant.LUMO_ICON);
MenuItem share = createIconItem(menuBar, VaadinIcon.SHARE, "Share", null);
SubMenu shareSubMenu = share.getSubMenu();
createIconItem(shareSubMenu, VaadinIcon.SHARE, "By Email", null, true);
createIconItem(shareSubMenu, VaadinIcon.LINK, "Get link", null, true);
createIconItem(menuBar, VaadinIcon.COPY, null, "duplicate");

...

private MenuItem createIconItem(HasMenuItems menu, VaadinIcon iconName, String label, String ariaLabel) {
    return createIconItem(menu, iconName, label, ariaLabel, false);
}
private MenuItem createIconItem(HasMenuItems menu, VaadinIcon iconName, String label, String ariaLabel, boolean isChild) {
    Icon icon = new Icon(iconName);

    if (isChild) {
        icon.getStyle().set("width", "var(--lumo-icon-size-s)");
        icon.getStyle().set("height", "var(--lumo-icon-size-s)");
        icon.getStyle().set("marginRight", "var(--lumo-space-s)");
    }

    MenuItem item = menu.addItem(icon, e -> {
    });

    if (ariaLabel != null) {
        item.getElement().setAttribute("aria-label", ariaLabel);
    }

    if (label != null) {
        item.add(new Text(label));
    }
    return item;
}
You can also try this code with Online Java Compiler
Run Code


Output:

O/P of the above program

 

Disabled Items

Menu items can be disabled to show that they are not available right now.

Code:

MenuBar menuBar = new MenuBar();

menuBar.addItem("View");
MenuItem edit = menuBar.addItem("Disabled");
edit.setEnabled(false);

MenuItem share = menuBar.addItem("Share");
SubMenu shareSubMenu = share.getSubMenu();
shareSubMenu.addItem("By email").setEnabled(false);
shareSubMenu.addItem("Get Link");
You can also try this code with Online Java Compiler
Run Code


Output:

O/P of the above program

 

Checkable Menu Items

Dropdown menu items may be configured to be checkable so that users can turn choices on and off.

Code:

MenuBar menuBar = new MenuBar();
MenuItem options = menuBar.addItem("Checkable Menu");
SubMenu subItems = options.getSubMenu();

MenuItem saveItem = subItems.addItem("Coding ninja");
saveItem.setCheckable(true);
saveItem.setChecked(true);
MenuItem notifyItem = subItems.addItem("Coding Ninjas Studio");
notifyItem.setCheckable(true);
notifyItem.setChecked(false);

saveItem.addClickListener(listener);
notifyItem.addClickListener(listener);
add(menuBar);
You can also try this code with Online Java Compiler
Run Code


Output:

O/P of the above program

Dividers

You can also use dividers in checkable menu items to show separate group items.

Code:

MenuBar menuBar = new MenuBar();
MenuItem item = menuBar.addItem("Share");
SubMenu subMenu = item.getSubMenu();
subMenu.addItem("On Facebook");
subMenu.addItem("On Twitter");
subMenu.addItem("On Instagram");
subMenu.add(new Hr());
subMenu.addItem("By email");
subMenu.addItem("Get link");
subMenu.add(new Hr());
subMenu.addItem("Visit Coding Ninjas Studio");
You can also try this code with Online Java Compiler
Run Code


Output:

O/P of the above program

 

Open on Hover

Dropdown menus can be customized to open on hover rather than on click in the component.

MenuBar menuBar = new MenuBar();
menuBar.setOpenOnHover(true);
addItems(menuBar);
add(menuBar);   

...

private void addItems(MenuBar menuBar) {
        MenuItem share = menuBar.addItem("Hover-over");
        SubMenu shareSubMenu = share.getSubMenu();
        MenuItem onSocialMedia = shareSubMenu.addItem("Coding ninjas");
        shareSubMenu.addItem("Coding Ninjas Studio");
}
You can also try this code with Online Java Compiler
Run Code


Output:

O/P of the above program

 

Keyboard Usage

Applications created using vaadin can be controlled using keyboard shortcuts. Some of those shortcuts are mentioned below.

Keyboard usage table

Frequently Asked Questions

What does a menu bar do?

A browser or application window's menu bar, which is normally located at the upper left corner, contains drop-down menus that let users interact with the content or application in a variety of ways.
 

Who is Vaadin for?

Vaadin is designed to improve full-stack teams' productivity. You may concentrate all your efforts on creating functionality thanks to full-stack type safety and smooth server-to-browser communication.
 

Is vaadin free?

Yes, it is accessible for both free and paid use. Because it is an open-source development platform, we can use it primarily for free. At a relatively little cost, we can also use it for business purposes.

Conclusion

In this article, we have extensively discussed vaadin-menu bar. We have also seen different features available in vaadin-menu bar.
 

If you think this blog has helped you enhance your knowledge about vaadin-menu bar and if you would like to learn more, check out our articles Vaadin-ButtonVaadin-BoardVaadin-Environment SetupVaadin Architecture, and many more on our Website.
 

Visit our website to read more such blogs. Make sure that you enroll in the courses provided by us, take mock tests and solve problems available and interview puzzles. Also, you can pay attention to interview stuff- interview experiences and an interview bundle for placement preparations. Do upvote our blog to help fellow ninjas grow.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Thank You

Please upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass