TreeGrid<Component> that can be used to embed & render mutliple components

Hello,
I’m using Vaadin 14 and want a TreeGrid that can handle Vaadin Components with object specific ContextMenus.

I have a dataProvider that creates Label objects, attaches a ContextMenu to the object and feeds the Stream to the TreeGrid.


public class MyObject extends components {

 String name;
 
public MyObject(String text) {
name = text;
ContextMenu menu = new ContextMenu();
menu.setTarget(this);
menu.addItem('Blah ' + text,null);

}

String<Component> fetchChildren() {

 List<MyObject> myObjects = ArrayList<MyObject>();
 
 myObjects.add(new MyObject("Matthew"));
 myObjects.add(new MyObject("James"));
 
 return myObjects.stream();

The tree gets rendered with the name but I don’t get a working ContextMenu

Hi! You shouldn’t return components from your data provider. The data provider should return only data, e.g. the names in your example, or objects that contain the names. Then you can define how to create components for each data item with a component renderer. See this example: https://vaadin.com/components/vaadin-grid/java-examples/using-components

Inside the method provided to the component renderer you can add a context menu for the component.

This way of creating a separate context menu for each component is quite heavy though. We are working on a feature that enables customizing the GridContextMenu based on the clicked item before opening: https://github.com/vaadin/vaadin-grid-flow/pull/748
It will probably be released in Vaadin 14.1.