Hi!

Could someone please explain me the difference between the following three approaches to inserting nodes in JTree? I cannot understand why the insertNodeInto method doesn't work in the recursive procedure (Approach Nr1), but it works if it is executed from JButton (Approach Nr2). So,

Recursion itself (Approach Nr1) works correctly - I checked it with System.out.println. The problem is exactly with insertNodeInto. The nodes can be inserted only to the root node. But if I want to add a sub-node to the node, then insertNodeInto fails.

I'm totally lost searching any ways to make Approach Nr1 working properly. Thanks for any idea.

Approach Nr1 - here insertNodeInto doesn't work...

root = new DefaultMutableTreeNode(hierarchy[0]);
tree1 = new JTree(root);
traverse( hierarchy, hierarchyChildOf, 0 );
...
public static void traverse( Object hierarchy[], Object hierarchyChildOf[], int k ) {
        for(int i = 0; i < hierarchy.length; i++) {
          if (hierarchy[k].equals(hierarchyChildOf[i])) {
             DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(hierarchy[i]);
             try {
                TreePath path = tree1.getNextMatch(hierarchyChildOf[i].toString(), 0, Position.Bias.Backward);
                if (path != null) {
                  DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
                  treeModel.insertNodeInto(childNode, parentNode, parentNode.getChildCount());
                  System.out.println("The child node " + hierarchy[i] + " is added to the parent node " + hierarchyChildOf[i]);
                } else {
                  System.out.println("The path to the parent node " + hierarchyChildOf[i] + " cannot be found.");
                }
             } catch (Exception e) {
                e.printStackTrace();
             }
             traverse(hierarchy, hierarchyChildOf, i);
          }
        }
    }

Approach Nr 2 - here insertNodeInto works!!!

Button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                 DefaultTreeModel model = (DefaultTreeModel)SystClasses.TreeEditTest.tree1.getModel();
        String nodeName = JOptionPane.showInputDialog(null, "Enter the node name:");
        if(nodeName.equals("")){
          JOptionPane.showMessageDialog(null, "Node is not added in the tree!");
        }
        else{
      //    create a new node
          DefaultMutableTreeNode nNode = new DefaultMutableTreeNode(nodeName);
          TreePath path = SystClasses.TreeEditTest.tree1.getNextMatch("Correspondence", 0, Position.Bias.Backward);
          MutableTreeNode node = (MutableTreeNode)path.getLastPathComponent();
          model.insertNodeInto(nNode, node, node.getChildCount());
          JOptionPane.showMessageDialog(null, "Node are added in the tree!");
            } }
        });

I had to expand the tree to solve the problem:

public static void expandAll(JTree tree) {
      int row = 0;
      while (row < tree.getRowCount()) {
        tree.expandRow(row);
        row++;
      }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.