1

List<List<CustomObject>>从 JTree获取

我正在根据这个问题填充JTree...递归填充JTree,使用列表列表

在此处输入图像描述

使用答案https://stackoverflow.com/a/56090968/7339033

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

使用类

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 + '}';
  }
}

现在我想List<List<Pair>> nestedListJTree人口中获得。

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

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

对我来说重要的问题:JTree,优化算法,Java

怎么做?

4

1 回答 1

1

使用同一个类

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}
于 2019-05-15T01:19:21.627 回答