When I try to create a Grid with a ContextMenu in the column header I get an “IllegalStateException: Can’t move a node from one state tree to another. If this is intentional, first remove the node from its current state tree by calling removeFromTree” when I click on the header after the first browser tab refresh.
It happens when the ContextMenu is added:
at com.vaadin.flow.component.UI.add(UI.java:961)
at com.vaadin.flow.component.contextmenu.ContextMenuBase.lambda$beforeOpen$504e9d67$1(ContextMenuBase.java:378)
at com.vaadin.flow.internal.StateTree.lambda$runExecutionsBeforeClientResponse$1(StateTree.java:368)
Here is a minimal working example:
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.contextmenu.ContextMenu;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.router.PreserveOnRefresh;
import com.vaadin.flow.router.Route;
@Route("")
@PreserveOnRefresh
public class MyPage extends Div {
public MyPage() {
Grid<String> grid=new Grid<>();
ContextMenu contextMenu=new ContextMenu(new Span("Click"));
contextMenu.setOpenOnClick(true);
contextMenu.addItem("Test");
grid.addColumn(s->s).setHeader(contextMenu.getTarget());
grid.setItems("A","B","C");
add(grid);
}
}