ContextMenu Addon

Hi,

I am exploring ways to introduce a contextmenu in my application. There’s a screen for which it would enrich the user experience to activate contextual options, rather than clicking on a button panel.

I discovered this address in a quick search which refers to a contextmenu, but I presume it is referred to another logic?

https://docs.jmix.io/api/1.0/ui/io/jmix/ui/widget/addon/contextmenu/ContextMenu.ContextMenuOpenListener.ContextMenuOpenEvent.html

What I am looking for is a way to enable the user right-clicking upon a screen element (mostly a label or image) and display a set of options depending on the element background logic.

I have searched for a contextmenu in the addon marketplace with no luck.

On the other hand I sort of implemented a similar logic with the PopupView widget, however I am not being able to position easily the popup element right beneath the triggering element.

Any ideas?

Many thanks.

Carlos.

Hello!

You can use ContextMenu extension. To apply it for components you should use unwrap() or withUnwrapped() methods. For instance:

@Autowired
private Label<String> label;

@Autowired
private Notifications notifications;

@Subscribe
public void onInit(InitEvent event) {
    label.withUnwrapped(JmixLabel.class, jmixLabel -> {
        ContextMenu contextMenu = new ContextMenu(jmixLabel, true);
        contextMenu.addItem("Item 1", selectedItem -> {
            notifications.create()
                    .withCaption(selectedItem.getText() + "- clicked")
                    .show();
        });
    });
}

image

Hi Roman,

Would that work with TreeTable? I would like to create my own ContextMenu.

Also do you think it is possible to implement a submenu in the context menu?

Thanks for your help.
Samy

Hello,

ContextMenu does not work correctly with Tables.

To implement submenu just add MenuItem to another MenuItem:

ContextMenu contextMenu = new ContextMenu(component, true);
MenuBar.MenuItem menuItem = contextMenu.addItem("Item 1", selectedItem -> {
    // do smth
});
menuItem.addItem("Sub item 2", (selectedItem) -> {
    // do smth
});

Thanks for your quick reply.

The following seems to work:

treeTable.withUnwrapped(JmixTreeTable.class, jmixTreeTable → {
TableContextMenu contextMenu = new TableContextMenu(jmixTreeTable);
contextMenu.addItem("item 1);
});

However the TableContextMenu is deprecated.

Are there any known issues using the approach above?

I have seen that you can compose menus/submenus, thanks.

Oh, I didn’t notice TableContextMenu class.

In my example, ContextMenu does not show a menu if you click on a row. And TableContextMenu solves the problem. It is deprecated as it is used for Vaadin compatibility components (table, treeTable). This functionality is not often used so there are no known issues.

Note, before using TableContextMenu you should disable treeTable’s context menu to show only custom menu:

treeTable.setContextMenuEnabled(false);

or in XML:

<treeTable id="treeTable"
           contextMenuEnabled="false"
1 Like