Results 1 to 7 of 7
  1. #1
    00jt is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default JTree error.. is it a bug?

    I'll try to simplify my problem... I have two different trees, both with the same hierarchy (they point to the same folder on my machine).

    Anyhow, the "TreePath" in one, does not seem to be allowed for the other one.

    What I want it to do is have them expanded the exact same way.
    I plan to do this by using "getExpandedDescendants()" on one tree, and using the collected Paths on the other Tree... but it can't find the paths.

    Below is a tiny exmple that shows the paths are (for some reason) NOT the same. I understand they are different objects, but you would think the Path would work for either tree (assuming they have the same paths).

    Java Code:
    public class Weird {
    
        public static void main(String [] args)
        {
           String myPath = "C:\\foo";
    
           JTree foo = new JTree(testSetup(new File(myPath)));
           JTree bar = new JTree(testSetup(new File(myPath)));
           TreePath fooPath = foo.getPathForRow(1);
           TreePath barPath = bar.getPathForRow(1);
    
           System.out.println("foo =" + fooPath.toString()); // print path
           System.out.println("bar =" + barPath.toString()); // print path
    
           int test1 = bar.getRowForPath(barPath); // check bar's row with a 'barPath'
           System.out.println("TEST1 = " + test1);
           int test2 = foo.getRowForPath(fooPath); // check foo's row with a 'fooPath'
           System.out.println("TEST2 = " + test2);
    
           int test3 = bar.getRowForPath(fooPath); // check bar's row with a 'fooPath'
           System.out.println("TEST3 = " + test3);
           int test4 = foo.getRowForPath(barPath); // check foo's row with a 'barPath'
           System.out.println("TEST4 = " + test4);
    
    
    
        }
    
        public static DefaultMutableTreeNode testSetup(File f)
        {
            DefaultMutableTreeNode root =
                    new DefaultMutableTreeNode(f.getName());
            File [] children;
            if (f.isDirectory())
            {
                children = f.listFiles();
                if(children.length != 0)
                {
                     for (File child : children) // Put Directories First
                    {
                        if(child.isDirectory())
                            root.add(testSetup(child));
                    }
                    for (File child: children) // add normal files after Directories
                    {
                         if(!child.isDirectory())
                             root.add(testSetup(child));
                    }
                }
                else
                {
                    root.add(testSetup(new File(""))); // add empty node to empty directories.
                }
            }
            return root;
        }
    
    }
    My Output

    Java Code:
    foo =[foo, foo1]
    bar =[foo, foo1]
    TEST1 = 1
    TEST2 = 1
    TEST3 = -1
    TEST4 = -1
    I would have liked "Test3" and "Test4" to be valid. ("-1" means not found, or the path to that node isn't fully expanded).

    - JT

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    The TreePath fooPath would not exist in the JTree bar, that is why you are receiving the -1's.

    Why not just try using the Row retrieve in the JTree foo, in the JTree bar? You could retrieve the Treepath in JTree bar by using the row retrieved from JTree foo using JTree bar's getPathFromRow() method.

  3. #3
    00jt is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by StormyWaters View Post
    The TreePath fooPath would not exist in the JTree bar, that is why you are receiving the -1's.
    I figured that much... but it still seems wrong.. I guess it comes down to what really constitutes a "path".

    Why not just try using the Row retrieve in the JTree foo, in the JTree bar? You could retrieve the Treepath in JTree bar by using the row retrieved from JTree foo using JTree bar's getPathFromRow() method.
    I have used the row in some cases... Perhaps i should expand my problem in more detail (pun intended).

    I have a single tree in my GUI that is a file system (as mentioned).
    I allow users to Add/Delete files/folders, and then re-load the tree from the root file when such events occur with the new file/folder.

    Consider the small tree example:

    Java Code:
    Foo/
       abc/
          myfile1.txt
          myfile1_1.txt
       xyz/
           myfile2.txt      
       myfile.txt
    Now if I wanted to delete "myfile1.txt" I can first get the expanded rows: (0,1,4).
    And i also notice that the selected file "myFile1.txt" is on row 2 (using "getRowForPath(getSelectionPath()) "), therefore when it gets deleted, all of the expanded rows with a row value over 2 will lose 1 row value: (0,1,3)

    So after the deletion i get:

    Java Code:
    Foo/
       abc/
          myfile1_1.txt
       xyz/
           myfile2.txt      
       myfile.txt
    Everything works fine with this.

    But i'm having a problem doing the same thing with ADDING a file/folder.
    Using the same example, lets say the user selects "Foo/" and wants to add a folder I don't know what row it will be put in. So i don't know how to change the row numbers. So what i wanted to do was (summary code below):

    Java Code:
    String myPath = "C:\\foo";
    oldTree = new JTree(testSetup(new File(myPath)));
    // ..... code to select new folder name and such...
    new File(myPath + File.separator + folderName).mkdir();
    // ...
    //... get the expanded rows of oldTree
    LinkedList<Integer> rows = ...
    
    // get new tree from file hierarchy (with the new folder)
    updatedTree = new JTree(testSetup(new File(myPath)));
    
    // get the TreePath from the oldTree to figure out where the new node will be inserted
    TreePath oldPath = oldTree.getSelectionPath();
    
    // below is my general idea on how to get the path, but I can't do it this way
    TreePath newNodePath = oldPath.pathByAddingChild(...); 
    
    // expand the nodes 
    
    int newRow =   theTree.getRowForPath(newNodePath);
    for(Integer i: rows)
      {
           
            if(i >= newRow  && selectedRow!= -1)
            {
                   updatedTree.expandRow(i+1);
            }
             else
             {
                     updatedTree.expandRow(i);
              }
      }

    Any ideas? The problem is that i don't know where the node will be inserted. And i don't know how to get Java to tell me.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

  5. #5
    00jt is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post

    for some reason i haven't been able to get on "forums.sun.com" from work.
    So i posted it here so that i discuss it while at work (the problem is related to a work project).

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by 00jt View Post
    for some reason i haven't been able to get on "forums.sun.com" from work.
    So i posted it here so that i discuss it while at work (the problem is related to a work project).
    It would be considered appropriate to inform us of this from the get-go, else you risk upsetting folks who volunteer free time to help, only to find out later that they wasted their time answering a question that was answered yesterday in a cross-post. I strongly urge you to list all cross-posts here.

    Edit: h&#101;ll, it was answered yesterday in the other forum! Lord help me jebus.

  7. #7
    00jt is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    It would be considered appropriate to inform us of this from the get-go, else you risk upsetting folks who volunteer free time to help, only to find out later that they wasted their time answering a question that was answered yesterday in a cross-post. I strongly urge you to list all cross-posts here.

    Edit: hell, it was answered yesterday in the other forum! Lord help me jebus.
    1) I wasn't aware that these forums were linked
    2) I actually posted that Question while at work, and then suddenly was no longer able to access forums.sun.com while at work (I tried several computers).
    3) sorry.

Similar Threads

  1. JTree/TreeModelListener
    By sQu3aKy in forum AWT / Swing
    Replies: 1
    Last Post: 04-28-2010, 07:47 AM
  2. JTree only display .txt
    By collin389 in forum AWT / Swing
    Replies: 2
    Last Post: 12-04-2009, 09:50 PM
  3. Jtree Help
    By miladirooni in forum New To Java
    Replies: 1
    Last Post: 10-28-2009, 12:00 AM
  4. zip to JTree
    By icsbcn in forum AWT / Swing
    Replies: 8
    Last Post: 09-01-2009, 04:26 PM
  5. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 11:59 AM

Posting Permissions

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