Step1-First I create a First Jtree.
Step2-Then using this Jtree I created set of JTrees and added to HashMap with some calculated weight value.
Step3-Then I select one JTree from this HashMap.
Step4-Now I want to repeat this from step2 to step3.
Befre repeat it I want to take only selected tree for future procedure. But It take final HashMap adding tree to reaping procedure. I updated same tree and added to HashMap.
public class JavaTree extends JFrame {
public static HashMap<String, DefaultMutableTreeNode> nodeReg = new HashMap<String, DefaultMutableTreeNode>();
public static void main(String[] args) {
}
public JTree method1() {
HashMap<JTree, Double> value = new HashMap<JTree, Double>();
//create tree root
DefaultMutableTreeNode Treeroot = new DefaultMutableTreeNode(new NodeInfor3(rootNode,x1));
//create tree
JTree firstTree = new JTree(Treeroot);
add(firstTree);
// create the child nodes
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(new NodeInfor3(nodeName1, x2));
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(new NodeInfor3(nodeName2), x3));
nodeReg.put(rootNode, Treeroot);
nodeReg.put(nodeName1), node1);
nodeReg.put(nodeName2, node2);
Treeroot.add(node1);
Treeroot.add(node2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("First JTree");
this.pack();
this.setVisible(true);
for (Map.Entry m1 : newNodeSet.entrySet()) {-----forloop X
for (Map.Entry m3 : TargetNodes.entrySet()) { -----forloop Y
DefaultMutableTreeNode targetNode = nodeReg.get(nodeName3);
targetNode.add(newAddingNode);
tree2 = new JTree(Treeroot);
add(tree2);
//ADD TO CALCULATION
WeghtOfSubTrees.put(tree2, calculated value));
}
//find maximum value with tree-using WeightOfSubTrees HashMap
I take selected Jtree name as a SELECTEDTREE
-------------------------------------------------------------------------
//THEN I WANT TO REPEAT. BUT WANT TO TAKE SELECTED TREE TO ABOVE PROCEDURE. IT TOOK FINAL VALUE OF HASHmAP.
tree2.removeAll(); -------------------------------???????
tree2 =SELECTEDTREE;-----------------------???????
-------------------------------------------------------------------------
WeghtOfSubTrees.clear();
}
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("New tree");
this.pack();
this.setVisible(true);
}
}
I included editing points in the above code. May be it will complex code. I think I want to assign new selected Jtree to first Jtree. I used following code part for it.
tree2.removeAll();
tree2 =SELECTEDTREE;
But, it don't work. How to assign new tree for first created tree before gotofor loop X
?
Based on the code you share you are adding the nodes to the existing root and creating tree based on the same root. I have created method to clone or the tree.
private JTree createTree(DefaultMutableTreeNode rootNode ) {
NodeInfor3 info =(NodeInfor3)rootNode.getUserObject();
//NodeInfor3 node = new NodeInfor3(info.name, info.getSpecVal());
DefaultMutableTreeNode newRoot = new DefaultMutableTreeNode(info);
JTree tree = new JTree(newRoot);
copyChildren(rootNode, newRoot);
return tree;
}
private void copyChildren(DefaultMutableTreeNode source , DefaultMutableTreeNode target) {
for (int i = 0; i < source.getChildCount(); i++) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)source.getChildAt(i);
NodeInfor3 info =(NodeInfor3)node.getUserObject();
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(info);
target.add(newNode);
if(node.isLeaf() == false){
copyChildren(node, newNode);
}
}
}
private void refreshNodeReg(DefaultMutableTreeNode root) {
NodeInfor3 node = (NodeInfor3)root.getUserObject();
nodeReg.put(node.getName(), root);
for (int i = 0; i < root.getChildCount(); i++) {
refreshNodeReg((DefaultMutableTreeNode)root.getChildAt(i));
}
}