Vaadin grid switch selection and open context menu on right-click

Hi,
I have a grid where each row has a context menu based on the selection. Users are complaining that they first have to left-click to select and then right-click to open the context menu. The common UX approach is that right-click should first select and then open context menu.

I have found different tips on using GridContextMenu.addGridContextMenuOpenedListener, but it seems too late since the context menu has already opened at that point.

Is there another trick in changing the behavior on right-click to first set selection in a Grid and then show context menu?

You can select row then right click/ Something like that:

 menu.addGridContextMenuOpenedListener(event -> {
            if (event.isFromClient()
                    && event.isOpened()) {
                event.getItem().ifPresent(i -> {
                    grid.select(i);
                    ...
                });
            }
        });

Yes, I tried that but the context menu is already shown and I can’t update it with the items it should display based on the new selection.

menu = grid.addContextMenu();
menu.setDynamicContentHandler( row -> buildYourMenuItems(row) );
1 Like