问题
Get List<List<CustomObject>>
from JTree
I was populating the JTree according to this question... Recursively populate JTree, using list of list
using the Answer 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)}));
using the 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 + '}';
}
}
Now I want to obtain List<List<Pair>> nestedList
from the JTree
populated.
Obviously the nestedList
must have the same items of listOfListPair
.
I was checking this post, but it's not working for me. http://www.java2s.com/Tutorial/Java/0240__Swing/VisitingAlltheNodesinaJTreeComponent.htm
Important Question for me:JTree, Optimization algorithm, Java
How to do it?
回答1:
using the same 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);
}
}
}
Now how to use?
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);
});
The output
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}
来源:https://stackoverflow.com/questions/56121754/get-nested-list-of-customobject-from-jtree-including-leaf