使用列表列表递归地填充JTree [英] Recursively populate JTree, using list of list
问题描述
此问题在从JTree获取嵌套的CustomObject列表(包括叶子)中具有相反的过程
我想实现过程的方法优化,我简化了这个过程,简化了(乘法和除法),就像显示的树一样.
I want to implement a method optimization of process, I reduced this process simple (multiplication and division) like a Tree shown.
底部和粗体矩形(在线工具不允许我更改颜色)表示每个列表的 N 分子和 D 分母之间的总和.
The Bottom and Bold Rectangles (the online tool don't let me change the color) represent the total multiplication between N Numerators and D Denominators of each list.
现在,我正在用Java创建列表列表,以便计算每个列表(流程)的总成本.
Now, I was creating list of list in Java, in order to calculate the total cost of each list (process).
我的第一步是创建一个类...
My first step was create a 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 + '}';
}
}
第二步是...
公共类TestPair {
public class TestPair {
public static void main(String... args) {
ArrayList<ArrayList<Pair>> listOfListPair = new ArrayList<>();
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(1, 2)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(3, 1)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(2, 1)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(1, 2)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(13, 3)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(1, 2)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 1)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(3, 1)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(5, 1)}));
listOfListPair.add((ArrayList<Pair>) Arrays.asList(
new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 7)}));
javax.swing.JTree myTree = new javax.swing.JTree();
listOfListPair.stream().forEachOrdered(listPair -> {
//HOW POPULATE THE myTree?
});
}
}
对我来说重要的问题: JTree,优化算法,Java
Important Question for me:JTree, Optimization algorithm, Java
是否有一些递归填充我的树的线索?,
推荐答案
我的回答几乎没有变化...
My answer with few changes...
public class TestPair {
public static void main(String... args) {
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)}));
final JTree tree = new JTree();
tree.setRootVisible(false);
removeAllTreeNodes(tree);
listOfListPair.stream().forEachOrdered(listPair -> {
addPairToTree(tree, listPair);
});
JFrame frame = new JFrame("Optimization Pair");
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(tree);
frame.add("Center", scrollPane);
frame.pack();
frame.setSize(new Dimension(600, 600));
frame.setVisible(true);
}
private static void addPairToTree(JTree tree, List<Pair> listPair) {
try {
DefaultTreeModel treeModel = ((DefaultTreeModel) tree.getModel());
DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) treeModel.getRoot();
DefaultMutableTreeNode pairTreeNode = rootTreeNode;
final Pair leafPair = new Pair(1, 1);
for (Pair pair : listPair) {
pairTreeNode = getDefaultMutableTreeNode(pairTreeNode, pair, true);
leafPair.setDen(leafPair.getDen() * ((Pair) pairTreeNode.getUserObject()).getDen());
leafPair.setNum(leafPair.getNum() * ((Pair) pairTreeNode.getUserObject()).getNum());
}
getDefaultMutableTreeNode(pairTreeNode, leafPair, false);
treeModel.reload(rootTreeNode);
} catch (Exception e) {
throw e;
}
}
private static DefaultMutableTreeNode getDefaultMutableTreeNode(DefaultMutableTreeNode parent, Pair newChild, Boolean isLeaf) {
if (parent != null) {
DefaultMutableTreeNode child;
for (int i = 0; i < parent.getChildCount(); i++) {
child = (DefaultMutableTreeNode) parent.getChildAt(i);
if (child.toString().equals(newChild.toString())) {
return child;
}
}
child = new DefaultMutableTreeNode(newChild, isLeaf);
parent.add(child);
return child;
} else {
return null;
}
}
private static void removeAllTreeNodes(JTree tree) {
DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) tree.getModel().getRoot();
if (rootTreeNode != null) {
rootTreeNode.removeAllChildren();
}
reloadTree(tree);
}
private static void reloadTree(JTree tree) {
DefaultTreeModel treeModel = ((DefaultTreeModel) tree.getModel());
DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) treeModel.getRoot();
treeModel.reload(rootTreeNode);
}
}
这篇关于使用列表列表递归地填充JTree的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!