如何将Swing TreeNode转换为Apache Tobago TreePath?

时间:2017-11-06 11:41:40

标签: java apache swing tree

我有一个Swing TreeNode(DefaultMutableTreeNode)并且必须为每个Swing TreeNode生成一个Apache Tobago TreePath:

摇摆树:

Root
    Node1
        Child11
        Child12
        Child13
    Node2
        Child21
        Child22
        Child23
    Node3
        Child31
        Child32
        Child33

Apache Tobago TreePath:

[]
    [0]
        [0,0]
        [0,1]
        [0,2]
    [1]
        [1,0]
        [1,1]
        [1,2]
    [2]
        [2,0]
        [2,1]
        [2,2]

示例:

  Input:  Child11
  Output: [0,1]

我们非常感谢任何建议。

提前致谢 托马斯

1 个答案:

答案 0 :(得分:2)

例如:

public static org.apache.myfaces.tobago.model.TreePath convertPath(TreeNode node) {
    List<Integer> list = new ArrayList<>();
    TreeNode current = node;
    while (current.getParent() != null) {
        list.add(0, current.getParent().getIndex(current));
        current = current.getParent();
    }
    return new org.apache.myfaces.tobago.model.TreePath(list);
}