Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

DefaultMutableTreeNode problem

  • 16-03-2004 11:42AM
    #1
    Closed Accounts Posts: 17,208 ✭✭✭✭


    Can anybody see whats wrong here?

    I have a Vector of Files, which I want to parse through and plot on a tree. The code I'm using is below. I decided to try to parse through the paths of the files, and placing each of them as children of the root node. The problem is when I return the root node all I get is a returned null value.

    I think the problem may lie in !root.isNodeDescendent(child), and I used this as the Javadoc tells me that "a node is considered a descendant of itself" - therefore if the root is null, it is not a descendant and the child should be added...

    Thanks in advance!
    public DefaultMutableTreeNode describeAsTree()
      {
        DefaultMutableTreeNode root = null;
        DefaultMutableTreeNode child;
        java.util.StringTokenizer parser;
        File individual;
    
        for(int i = 0; i < fileStructure.size(); i++)
        {
          individual = (File)fileStructure.elementAt(i);
          parser = new java.util.StringTokenizer(individual.getAbsolutePath(), "/");
    
          while(parser.hasMoreTokens())
          {
            child = new DefaultMutableTreeNode(parser.nextToken());
            if(!root.isNodeDescendant(child))
              root.add(child);
    
            root = child;
          }
        }
    
        return root;
      }
    


Comments

  • Closed Accounts Posts: 1,525 ✭✭✭vorbis


    I'd say the line root = child is a bit wonky. You're setting the root equal to the child with that line. I presume that you want to return the overall root. Also you should use system.out.println() to check if if(!root.isNodeDescendant(child)) is working. Not sure after that.


Advertisement