Vaadin 8 TreeGrid renders empty

I am trying to create a TreeGrid as in the [Vaadin Framework 8 TreeGrid page]
(https://vaadin.com/docs/v8/framework/components/components-treegrid.html#components.treegrid), but the result is empty when rendered. Below is my code and attached is the result I get in the browser:

public class TestView extends VerticalLayout implements View {

    private static final Logger LOGGER = LoggerFactory.getLogger(TestView.class);

    private TreeGrid<Project> treeGrid = new TreeGrid<>();
    private List<Project> projects = new ArrayList<>();


    @PostConstruct
    public void init() {
        setSpacing(true);
        setSizeFull();
        initProjects();
        setupTreeGrid();
    }

    private void setupTreeGrid() {
        treeGrid.setSizeFull();
        loadTreeGridData();
        addComponent(treeGrid);
    }

    private void loadTreeGridData() {

        **// Initialize a TreeGrid and set in-memory data
        TreeGrid<Project> treeGrid = new TreeGrid<>();
        treeGrid.setItems(getRootProjects(), Project::getSubProjects);

        // The first column gets the hierarchy indicator by default
        treeGrid.addColumn(Project::getName).setCaption("Project Name");
        treeGrid.addColumn(Project::getHoursComplete).setCaption("Hours Done");
        treeGrid.addColumn(Project::getLastModified).setCaption("Last Modified");**
    }

    private void initProjects() {
        Project p1 = new Project("p1", 20, LocalDateTime.now(), new ArrayList<>(), true);
        Project p11 = new Project("p11", 12, LocalDateTime.now(), new ArrayList<>(), false);
        Project p12 = new Project("p12", 8, LocalDateTime.now(), new ArrayList<>(), false);
        p1.setSubprojects(Arrays.asList(p11, p12));

        Project p2 = new Project("p2", 11, LocalDateTime.now(), new ArrayList<>(), true);
        Project p21 = new Project("p3", 11, LocalDateTime.now(), new ArrayList<>(), false);
        p2.setSubprojects(Arrays.asList(p21));

        Project p3 = new Project("p3", 34, LocalDateTime.now(), new ArrayList<>(), true);

        projects.addAll(Arrays.asList(p1, p11, p12, p2, p21));
    }

    private List<Project> getRootProjects() {
        return projects
                .stream()
                .filter(Project::isRoot)
                .collect(Collectors.toList());
    }


    public class Project {
        String name;
        Integer hoursComplete;
        LocalDateTime lastModified;
        private List<Project> subprojects;
        boolean isRoot;

        public Project(String name, Integer hoursComplete, LocalDateTime lastModified, List<Project> subprojects, boolean isRoot) {
            this.name = name;
            this.hoursComplete = hoursComplete;
            this.lastModified = lastModified;
            this.subprojects = subprojects;
            this.isRoot = isRoot;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getHoursComplete() {
            return hoursComplete;
        }

        public void setHoursComplete(Integer hoursComplete) {
            this.hoursComplete = hoursComplete;
        }

        public LocalDateTime getLastModified() {
            return lastModified;
        }

        public void setLastModified(LocalDateTime lastModified) {
            this.lastModified = lastModified;
        }

        public List<Project> getSubProjects() {
            return subprojects;
        }

        public void setSubprojects(List<Project> subprojects) {
            this.subprojects = subprojects;
        }

        public boolean isRoot() {
            return isRoot;
        }

        public void setRoot(boolean root) {
            isRoot = root;
        }
    }
}

17069436.png

Looks like you need to use TreeDataProvider since you are dealing with hierarchical data.
https://vaadin.com/docs/v8/framework/datamodel/datamodel-hierarchical.html#datamodel.hierarchical

Thank you Graham for your reply. In fact it turns out that I am creating an entirely new TreeGrid object in my code. This is a sweet mistake :slight_smile:

Is:

private void loadTreeGridData() {
...
	TreeGrid<Project> treeGrid = new TreeGrid<>();
    treeGrid.setItems(getRootProjects(), Project::getSubProjects);

Should be:

private void loadTreeGridData() {
...
    treeGrid.setItems(getRootProjects(), Project::getSubProjects);

Ahh, good catch!