Since there are no built-in functions for this (as indicated in the comments of @kleopatra), I came up with the following TreeItem implementation:
public class AutomatedTreeItem<C, D> extends TreeItem<D> { public AutomatedTreeItem(C container, Function<C, D> dataFunction, Function<C, Collection<? extends C>> childFunction) { super(dataFunction.apply(container)); getChildren().addAll(childFunction.apply(container) .stream() .map(childContainer -> new AutomatedTreeItem<C, D>(childContainer, dataFunction, childFunction)) .collect(Collectors.toList())); } }
Usage example:
Function<MyData, MyData> dataFunction = c -> c; Function<MyData, Collection<? extends MyData>> childFunction = c -> c.getChildren(); treeTableView.setRoot(new AutomatedTreeItem<MyData, MyData>(myRootData, dataFunction, childFunction));
This will probably help someone in the future.
source share