Results 1 to 3 of 3
  1. #1
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default The insertNodeInto method doesn't work for my JTree

    Hi!


    Initially, I have a data set stored in the database and a BLANK JTree. This data set contains a tree structure, e.g.:
    1. Documents
    1.1 Correspondence
    1.1.1 Incoming
    1.1.2 Outgoing
    1.2 Contracts
    etc.

    What I need, is to build the JTree based on the above-shown information. The problem is that insertNodeInto doesn't work properly in my code - only second-level nodes are added to the root node, while the path to the other nodes is not found. Where is the problem? Thanks!

    PHP Code:
    #
    public static void traverse( Object hierarchy[], Object hierarchyChildOf[], int k ) {
    for(int i = 0; i < hierarchy.length; i++) {
    if (hierarchy[k].equals(hierarchyChildOf[i])) {
      MutableTreeNode childNode = new DefaultMutableTreeNode(hierarchy[i]);
      try {
        TreePath path = tree1.getNextMatch(hierarchyChildOf[i].toString(), 0, Position.Bias.Forward);
         if (path != null) {
            MutableTreeNode parentNode = (DefaultMutableTreeNode)  (MutableTreeNode) 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);
       }
     }
    }
    
          public static void createTree() {
             ...
             tree1 = new JTree(); tree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
             root = new DefaultMutableTreeNode(hierarchy[0]);
             treeModel = (DefaultTreeModel)tree1.getModel();
             treeModel.setRoot(root);
             traverse( hierarchy, hierarchyChildOf, 0 );
             ...
          }
    This is output of System.out.println
    PHP Code:
    The child node Contracts is added to the parent node Documents
    The child node Correspondence is added to the parent node Documents
    The path to the parent node Correspondence cannot be found.
    The path to the parent node Correspondence cannot be found.
    The child node Inner documents is added to the parent node Documents
    The child node Labour contracts is added to the parent node Documents
    Last edited by LianaN; 11-30-2010 at 01:36 PM.

  2. #2
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    I'll try to be more specific.

    There are two arrays filled from DB:
    * hierarchy[]: Documents, Correspondence, Incoming, Outgoing, Contracts
    * hierarchyChildOf[]: null, Documents, Correspondence, Correspondence, Documents

    For instance, hierarchy[1] (Correspondence) is a child of hierarchyChildOf[2] (Documents). Now I must populate JTree with these data. The task is just to add all nodes defined in hierarchy[] to corresponding parent nodes specified in hierarchyChildOf[].

    Below you may find my comments on the code:

    PHP Code:
          public static void traverse( Object hierarchy[], Object hierarchyChildOf[], int k ) {
          for(int i = 0; i < hierarchy.length; i++) {
          // We are searching children of hierarchy[k].
          // If hierarchy[k] is equal to hierarchyChildOf[i], then it means that
          // hierarchy[i] is a child of hierarchy[k]
          if (hierarchy[k].equals(hierarchyChildOf[i])) {
          MutableTreeNode childNode = new DefaultMutableTreeNode(hierarchy[i]);
          try {
          // We are searching the node hierarchyChildOf[i] in the JTree->tree1
          TreePath path = tree1.getNextMatch(hierarchyChildOf[i].toString(), 0, Position.Bias.Forward);
          if (path != null) {
          // If such node exists, then the childNode hierarchy[i] is added
          // to the parentNode hierarchyChildOf[i]
          MutableTreeNode parentNode = (DefaultMutableTreeNode)(MutableTreeNode) 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);
          }
          }
          }
    Any ideas how to implement it correctly?

  3. #3
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    I've solved the problem in the following way:

    PHP Code:
            Hashtable h = new Hashtable();
            for (int i = 0; i < hierarchy.length; i++) {
               Hashtable child = new Hashtable();
               for(int j = 0; j < hierarchy.length; j++) {
                  if (hierarchy[i].equals(hierarchyChildOf[j])) {
                      child.put(hierarchy[j],hierarchy[j]);
                  }
               }
               if (!child.isEmpty()) h.put(hierarchy[i], child);
            }
    
            tree1 = new JTree(h);

Similar Threads

  1. Replies: 13
    Last Post: 05-16-2010, 12:09 AM
  2. help~ delete method cant work
    By reeveliew in forum New To Java
    Replies: 4
    Last Post: 05-07-2010, 02:24 AM
  3. why doesnt my insertion sort method not work?
    By Jeremy8 in forum New To Java
    Replies: 7
    Last Post: 11-15-2009, 02:56 AM
  4. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 11:59 AM
  5. how does the remove method work for sets and hashsets
    By haridharna in forum Advanced Java
    Replies: 4
    Last Post: 08-06-2007, 12:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •