本文介绍了从JTree获取CustomObject的嵌套列表,包括叶子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从JTree获取List<List<CustomObject>>

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

I was populating the JTree according to this question... Recursively populate JTree, using list of list

使用答案 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 + '}';
  }
}

现在,我想从填充的JTree中获取List<List<Pair>> nestedList.

Now I want to obtain List<List<Pair>> nestedList from the JTree populated.

很显然,nestedList必须具有与listOfListPair相同的项目.

Obviously the nestedList must have the same items of listOfListPair.

我正在查看此信息,但它对我不起作用. http://www.java2s.com/Tutorial/Java/0240__Swing/VisitingAlltheNodesinaJTreeComponent. htm

I was checking this post, but it's not working for me. http://www.java2s.com/Tutorial/Java/0240__Swing/VisitingAlltheNodesinaJTreeComponent.htm

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

Important Question for me:JTree, Optimization algorithm, Java

如何做到?

推荐答案

使用相同的类

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}

这篇关于从JTree获取CustomObject的嵌套列表,包括叶子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 04:47