Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

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