gpt4 book ai didi

java - 获取 BeanTreeView 中的所有节点

转载 作者:行者123 更新时间:2023-12-02 06:39:48 25 4
gpt4 key购买 nike

我有一个 BeanTreeView 显示一些节点。我还有另一个与 netbeans 无关的组件,它需要知道此时树中的内容。最好只是树中节点的直接列表。 (澄清一下;当我在这里谈论节点时,我指的是 netbeans 节点,而不是 JTree 中的 TreeNode。)

我还没有找到任何有用的方法来做到这一点。我似乎无法从连接到 BeanTreeView 的关联 ExplorerManager 获取信息。到目前为止,我已经对 BeanTreeView 进行了子类化并添加了私有(private)方法

private List<Object> getNodesAsList(){
LinkedList<Object> result = new LinkedList<>();
for (int i = 0; i < tree.getRowCount(); i++) {
TreePath pathForRow = tree.getPathForRow(i);
Object lastPathComponent = pathForRow.getLastPathComponent();
result.add(lastPathComponent);
}
return result;
}

我从getLastPathComponent得到的对象是一个VisualizerNode,它保存着我想要获取的节点。但我无法转换为 VisualizerNode,因为它在 org.openide.explorer.view 中不是公开的。这是最终的。无论如何,节点没有 setter/getter ......

有什么想法吗?我觉得 ExplorerManager 遗漏了一些东西...

更新;这对我有用,可能可以做得更优雅。谢谢稻田!

private List<Node> getNodesInTree(){
LinkedList<Node> result = new LinkedList<>();
ExplorerManager em = ExplorerManager.find(this);
for (Node node : em.getRootContext().getChildren().getNodes()) {
result.add(node);
result.addAll(getChildNodesInTree(node));
}
return result;
}

private List<Node> getChildNodesInTree(Node root){
LinkedList<Node> result = new LinkedList<>();
if(root.getChildren().getNodesCount() > 0){
if(isExpanded(root)){
for (Node node : root.getChildren().getNodes()) {
result.add(node);
result.addAll(getChildNodesInTree(node));
}
}
}
return result;
}

最佳答案

您可以使用ExplorerManager.getRootContext() 。这将返回 ExplorerManager 中显示的根节点。从这个节点,您可以迭代所有其他节点(使用 Node.getChildren() )并创建您自己的列表。我不知道有哪个函数可以为您处理这个问题。

关于java - 获取 BeanTreeView 中的所有节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19266248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com