Equal/Assign Java 中的两个 JTrees

Equal/Assign two JTrees in Java

第 1 步 - 首先创建第一个 Jtree。

第 2 步 - 然后使用这个 Jtree 我创建了一组 JTrees 并使用一些计算的权重值添加到 HashMap。

Step3-然后我 select 来自这个 HashMap 的一个 JTree。

第 4 步-现在我想从第 2 步到第 3 步重复此操作。

在重复之前,我只想为以后的程序取 selected 树。 但是它需要最终的 HashMap 添加树来收割过程。我更新了同一棵树并添加到 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);

            }
        }

我在上面的代码中加入了编辑点。可能会是复杂的代码。我想我想将新的 selected Jtree 分配给第一个 Jtree。我为此使用了以下代码部分。

                    tree2.removeAll(); 
                    tree2 =SELECTEDTREE;

但是,它不起作用。如何在 gotofor loop X?

之前为第一个创建的树分配新树

根据您共享的代码,您正在将节点添加到现有根并基于同一根创建树。我已经创建了克隆或树的方法。

    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));
        }
    }