从 JTree 获取嵌套的 CustomObject 列表,包括叶

get nested List of CustomObject from JTree, including leaf

从 JTree

获取 List<List<CustomObject>>

我正在根据这个问题填充 JTree...

使用答案

    List<List<Pair>> listOfListPair = new ArrayList<>();

    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(1, 2)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(3, 1)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(2, 1)}));

    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(1, 2)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(13, 3)}));

    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(1, 2)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 1)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(3, 1)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(5, 1)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 7)}));

使用 class

public class Pair {

  private int num;
  private int den;

  public Pair(int num, int den) {
    this.num = num;
    this.den = den;
  }

  public int getNum() {
    return num;
  }

  public void setNum(int num) {
    this.num = num;
  }

  public int getDen() {
    return den;
  }

  public void setDen(int den) {
    this.den = den;
  }

  @Override
  public String toString() {
    return "Pair{" + "num=" + num + ", den=" + den + '}';
  }
}

现在我想从 JTree 中获取 List<List<Pair>> nestedList

显然 nestedList 必须具有与 listOfListPair 相同的项目。

我正在检查这个 post,但它对我不起作用。 http://www.java2s.com/Tutorial/Java/0240__Swing/VisitingAlltheNodesinaJTreeComponent.htm

对我来说重要的问题:JTree, Optimization algorithm, Java

怎么做?

使用相同的 class

private static List<List<Pair>> getListPairFromTree(JTree tree) {
  List<List<Pair>> listOfListPair = new ArrayList<>();
  List<DefaultMutableTreeNode> listLeafDefaultMutableTreeNode = getListLeafsFromTree(tree);
  listLeafDefaultMutableTreeNode.stream().forEach(leaf -> {
    List<Pair> listPair = new ArrayList<>();
    listPair.add((Pair) leaf.getUserObject());
    TreeNode treeNode = leaf.getParent();
    while (treeNode != null) {
      DefaultMutableTreeNode parent = (DefaultMutableTreeNode)treeNode;
      if ((parent).getUserObject() instanceof Pair) {
        listPair.add(0, (Pair) parent.getUserObject());
      }
      treeNode = parent.getParent();
    }
    listOfListPair.add(listPair);
  });
  return listOfListPair;
}

private static List<DefaultMutableTreeNode> getListLeafsFromTree(JTree tree) {
  DefaultTreeModel treeModel = ((DefaultTreeModel) tree.getModel());
  DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) treeModel.getRoot();
  List<DefaultMutableTreeNode> listLeafDefaultMutableTreeNode = new ArrayList<>();
  getLeafs(rootTreeNode, listLeafDefaultMutableTreeNode);
  return listLeafDefaultMutableTreeNode;
}

private static void getLeafs(DefaultMutableTreeNode parent, List<DefaultMutableTreeNode> listLeafDefaultMutableTreeNode) {
  if (parent.isLeaf()) {
    listLeafDefaultMutableTreeNode.add(parent);
  } else {
    for (int i = 0; i < parent.getChildCount(); i++) {
      DefaultMutableTreeNode child = (DefaultMutableTreeNode) parent.getChildAt(i);
      getLeafs(child, listLeafDefaultMutableTreeNode);
    }
  }
}

现在怎么用?

System.out.println("\nList of Leaf");
getListLeafsFromTree(tree).stream().forEach(System.out::println);

System.out.println("\nList<List<Pair>>");
getListPairFromTree(tree).stream().forEach(listPair -> {
  System.out.println();
  listPair.stream().forEach(System.out::print);
});

输出

    List of Leaf
    Pair{num=3, den=2}
    Pair{num=9, den=1}
    Pair{num=6, den=1}
    Pair{num=11, den=4}
    Pair{num=143, den=6}
    Pair{num=1, den=14}
    Pair{num=4, den=7}
    Pair{num=6, den=7}
    Pair{num=10, den=7}
    Pair{num=4, den=49}

    List<List<Pair>>

    Pair{num=1, den=1}Pair{num=3, den=1}Pair{num=1, den=2}Pair{num=3, den=2}
    Pair{num=1, den=1}Pair{num=3, den=1}Pair{num=3, den=1}Pair{num=9, den=1}
    Pair{num=1, den=1}Pair{num=3, den=1}Pair{num=2, den=1}Pair{num=6, den=1}
    Pair{num=1, den=1}Pair{num=11, den=2}Pair{num=1, den=2}Pair{num=11, den=4}
    Pair{num=1, den=1}Pair{num=11, den=2}Pair{num=13, den=3}Pair{num=143, den=6}
    Pair{num=1, den=1}Pair{num=1, den=7}Pair{num=1, den=2}Pair{num=1, den=14}
    Pair{num=1, den=1}Pair{num=1, den=7}Pair{num=2, den=1}Pair{num=2, den=1}Pair{num=4, den=7}
    Pair{num=1, den=1}Pair{num=1, den=7}Pair{num=2, den=1}Pair{num=3, den=1}Pair{num=6, den=7}
    Pair{num=1, den=1}Pair{num=1, den=7}Pair{num=2, den=1}Pair{num=5, den=1}Pair{num=10, den=7}
    Pair{num=1, den=1}Pair{num=1, den=7}Pair{num=2, den=1}Pair{num=2, den=7}Pair{num=4, den=49}