javaanchorcontextmenuvaadin14vaadin-grid

Is there a way to put a anchor component in an context menu in vaadin 14?


Im trying to get a vaadin anchor component inside of an context menu, but I cant get it to work. I tried it like following:

this.contextMenu.add(anchorFile);

But the menu item inside of the context menu wont appear.

I hope someone can help me. Thanks in advance


Solution

  • Have you set the text content for your Anchor component?

    The proper way to add the Anchor as menu item is this.

        ContextMenu menu = new ContextMenu(targetComponent);
        Anchor vaadin = new Anchor("https://vaadin.com/","Vaadin");
        menu.addItem(vaadin);
    

    add methods also adds the component to the menu, but wont't wrap as menu item. It is more purposed for adding decorative components like dividers etc.

        ContextMenu menu = new ContextMenu(targetComponent);
        Anchor vaadin = new Anchor("https://vaadin.com/","Vaadin");
        menu.add(vaadin);
    

    Moreover with addIten method you can set the event listener for menu item being selected:

        ContextMenu menu = new ContextMenu(targetComponent);
        Anchor vaadin = new Anchor("https://vaadin.com/","Vaadin");
        menu.addItem(vaadin, event -> {
            System.out.println("Selected");
        });