是否可以根据函数动态生成 JavaFX TreeItem 的 children?
Is it possible to generate a JavaFX TreeItem's children dynamically based on a function?
简介:
我目前正在开发我的第一个 TreeView
JavaFX。
文档中给出的示例如下:
TreeItem<String> root = new TreeItem<String>("Root Node");
root.setExpanded(true);
root.getChildren().addAll(
new TreeItem<String>("Item 1"),
new TreeItem<String>("Item 2"),
new TreeItem<String>("Item 3")
);
TreeView<String> treeView = new TreeView<String>(root);
在此示例中,我们手动构建 TreeItem
树结构,即在每个具有 children 的节点上调用 getChildren()
并添加这些。
问题:
是否可以告诉 TreeItem
到 "dynamically" 构建它的 children?如果我能将 parent-child-relationship 定义为一个函数就完美了。
我会寻找如下内容:
// Function that generates the child tree items for a given tree item
Function<TreeItem<MyData>, List<TreeItem<MyData>>> childFunction = parent -> {
List<TreeItem<MyData>> children = new ArrayList<>(
parent. // TreeItem<MyData>
getValue(). // MyData
getChildrenInMyData(). // List<MyData>
stream().
map(myDataChild -> new TreeItem<MyData>(myDataChild)))); // List<TreeItem<MyData>>
// The children should use the same child function
children.stream().forEach(treeItem -> treeItem.setChildFunction(childFunction));
return children;
};
TreeItem<MyData> root = new TreeItem<MyData>(myRootData);
root.setExpanded(true);
// THE IMPORTANT LINE:
// Instead of setting the children via .getChildren().addAll(...) I would like to set a "child function"
root.setChildFunction(childFunction);
TreeView<MyData> treeView = new TreeView<String>(root);
由于没有内置功能(正如@kleopatra 在评论中指出的那样),我提出了以下 TreeItem
实现:
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()));
}
}
用法示例:
Function<MyData, MyData> dataFunction = c -> c;
Function<MyData, Collection<? extends MyData>> childFunction = c -> c.getChildren();
treeTableView.setRoot(new AutomatedTreeItem<MyData, MyData>(myRootData, dataFunction, childFunction));
这可能会对以后的人有所帮助。
简介:
我目前正在开发我的第一个 TreeView
JavaFX。
文档中给出的示例如下:
TreeItem<String> root = new TreeItem<String>("Root Node");
root.setExpanded(true);
root.getChildren().addAll(
new TreeItem<String>("Item 1"),
new TreeItem<String>("Item 2"),
new TreeItem<String>("Item 3")
);
TreeView<String> treeView = new TreeView<String>(root);
在此示例中,我们手动构建 TreeItem
树结构,即在每个具有 children 的节点上调用 getChildren()
并添加这些。
问题:
是否可以告诉 TreeItem
到 "dynamically" 构建它的 children?如果我能将 parent-child-relationship 定义为一个函数就完美了。
我会寻找如下内容:
// Function that generates the child tree items for a given tree item
Function<TreeItem<MyData>, List<TreeItem<MyData>>> childFunction = parent -> {
List<TreeItem<MyData>> children = new ArrayList<>(
parent. // TreeItem<MyData>
getValue(). // MyData
getChildrenInMyData(). // List<MyData>
stream().
map(myDataChild -> new TreeItem<MyData>(myDataChild)))); // List<TreeItem<MyData>>
// The children should use the same child function
children.stream().forEach(treeItem -> treeItem.setChildFunction(childFunction));
return children;
};
TreeItem<MyData> root = new TreeItem<MyData>(myRootData);
root.setExpanded(true);
// THE IMPORTANT LINE:
// Instead of setting the children via .getChildren().addAll(...) I would like to set a "child function"
root.setChildFunction(childFunction);
TreeView<MyData> treeView = new TreeView<String>(root);
由于没有内置功能(正如@kleopatra 在评论中指出的那样),我提出了以下 TreeItem
实现:
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()));
}
}
用法示例:
Function<MyData, MyData> dataFunction = c -> c;
Function<MyData, Collection<? extends MyData>> childFunction = c -> c.getChildren();
treeTableView.setRoot(new AutomatedTreeItem<MyData, MyData>(myRootData, dataFunction, childFunction));
这可能会对以后的人有所帮助。