I’ve a grid and I added the context menu.
The context menu is shown but clicking on the item the event doesn’t fire.
I tryed also with the fluent coding but nothing changed.
What’s wrong ?
Here the code
GridContextMenu contextMenu = getGrid().addContextMenu();
ComponentEventListener<GridContextMenuItemClickEvent> listener2 = new ComponentEventListener<> () {
@Override
public void onComponentEvent(GridContextMenuItemClickEvent xpEvent) {
E entita = (E) xpEvent.getItem().orElse(null);
System.out.println(entita.toString());
}
};
contextMenu.addItem(“Show name of context menu target item”, listener2);
At least the generics for ComponentEventListener<GridContextMenuItemClickEvent> listener2
and GridContextMenuItemClickEvent xpEvent
are wrong. The correct type depends on your Grid’s type, but maybe something like ComponentEventListener<GridContextMenuItemClickEvent<E>>
. Otherwise, it seems ok; here’s a similar but simplified example with a String
grid that works for me:
Grid<String> grid = new Grid<>();
grid.addColumn(s -> s).setHeader("Yes");
GridContextMenu<String> gridContextMenu = grid.addContextMenu();
ComponentEventListener<GridContextMenuItemClickEvent<String>> listener = new ComponentEventListener<>() {
@Override
public void onComponentEvent(GridContextMenuItemClickEvent<String> xpEvent) {
String entita = (String) xpEvent.getItem().orElse(null);
System.out.println(entita.toString());
}
};
gridContextMenu.addItem("Hello", listener);
grid.setItems("foo", "bar", "baz", "quux");
But it doesn’t work.
I’m using Vaadin 23.3.21.
Is it a bug ?
I tested the snippet posted by Olli with 23.3.21 and it works fine for me.
BTW, 23.3.21 is an ancient version for the V23 series.
23.3 is not supported anymore, but the last released version is 23.3.35
At present I can’t change the Vaadin version due to an underling dependency (Holon Platform).
The upgrade of that platform is under way but I need to try to solve now.
It seems as another event would prevent the context menu event to fire.
Could You suggest the way to investigate that issue ?
First of all, I would put a break point in the listener event to see if it gets hit.
On the client side you can check browser console and network tab.
When right clicking and pressing the menu item button you should see a couple of “Sending xhr message to server …” messages and relative HTTP request
On right click, you should find a message with "event":"vaadin-context-menu-before-open"
Then, when clicking the menu item you should see the click message with an "event": "click"
entry, similar to the following one
{"csrfToken":".......","rpc":[
{"type":"event","node":12,"event":"click"},
{"type":"mSync","node":9,"feature":1,"property":"opened","value":false},
{"type":"event","node":9,"event":"opened-changed","data":{}}
],"syncId":7,"clientId":7}
This is what I see when I click on the item of the context menu :
Sending xhr message to server: {“csrfToken”:“17fd06d8-13a0-4ea8-9d2b-4124dc3a87bc”,“rpc”:[{“type”:“mSync”,“node”:391,“feature”:1,“property”:“opened”,“value”:false},{“type”:“event”,“node”:391,“event”:“opened-changed”,“data”:{}},{“type”:“mSync”,“node”:393,“feature”:1,“property”:“opened”,“value”:false},{“type”:“event”,“node”:393,“event”:“opened-changed”,“data”:{}}],“syncId”:32,“clientId”:32}
An information more : using keyboard it works.
Clicking outside the context menu I see the same sent message.
I’m sorry, but I have no idea. For me, it works without any issue with 23.3.21.
It looks like something is “eating” the click event on the context menu item, but without a reproducible example, it is quite difficult to make any guess.