简体   繁体   English

是否可以基于函数动态生成JavaFX TreeItem的子项?

[英]Is it possible to generate a JavaFX TreeItem's children dynamically based on a function?

Intro: 介绍:

I am currently working on my first TreeView in JavaFX. 我目前正在使用JavaFX中的第一个TreeView

The example given in the documentation is the following: 文档中给出的示例如下:

 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);

In this example, we build the TreeItem tree structure manually, ie, calling getChildren() on every node that has children and adding these. 在这个例子中,我们手动构建TreeItem树结构,即在每个有子节点的节点上调用getChildren()并添加它们。

Question: 题:

Is it possible to tell a TreeItem to "dynamically" build its children? 是否有可能告诉TreeItem “动态”构建其子项? It would be perfect if I could define the parent-child-relationship as a function. 如果我可以将父子关系定义为函数,那将是完美的。

I would be looking for something like the following: 我会寻找类似以下的东西:

// 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);

Since there is no built-in functionality for this (as pointed out by @kleopatra in the comments), I came up with the following TreeItem implementation: 由于没有内置功能(正如@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()));
    }
}

Example usage: 用法示例:

Function<MyData, MyData> dataFunction = c -> c;
Function<MyData, Collection<? extends MyData>> childFunction = c -> c.getChildren();

treeTableView.setRoot(new AutomatedTreeItem<MyData, MyData>(myRootData, dataFunction, childFunction));

Probably this will help someone in the future. 可能这将有助于未来的某些人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM