从JTree获取List<List<CustomObject>>
我正在根据此问题填充JTree ... 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 + '}';
}
}
现在,我想从填充的List<List<Pair>> nestedList
中获取JTree
。
显然,nestedList
必须与listOfListPair
具有相同的项目。
我正在查看此信息,但它对我不起作用。 http://www.java2s.com/Tutorial/Java/0240__Swing/VisitingAlltheNodesinaJTreeComponent.htm
如何做到?
答案 0 :(得分:0)
使用同一班
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}