Results 1 to 7 of 7
Thread: JTree error.. is it a bug?
- 06-15-2010, 09:19 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
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).
My OutputJava 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; } }
I would have liked "Test3" and "Test4" to be valid. ("-1" means not found, or the path to that node isn't fully expanded).Java Code:foo =[foo, foo1] bar =[foo, foo1] TEST1 = 1 TEST2 = 1 TEST3 = -1 TEST4 = -1
- JT
- 06-15-2010, 10:07 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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.
- 06-15-2010, 11:17 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
I figured that much... but it still seems wrong.. I guess it comes down to what really constitutes a "path".
I have used the row in some cases... Perhaps i should expand my problem in more detail (pun intended).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 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:
Now if I wanted to delete "myfile1.txt" I can first get the expanded rows: (0,1,4).Java Code:Foo/ abc/ myfile1.txt myfile1_1.txt xyz/ myfile2.txt myfile.txt
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:
Everything works fine with this.Java Code:Foo/ abc/ myfile1_1.txt xyz/ myfile2.txt myfile.txt
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.
- 06-15-2010, 11:56 PM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Cross posted: Java Programming - JTree error.. is it a bug?
- 06-16-2010, 12:12 AM #5
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
- 06-16-2010, 01:39 AM #6
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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.
- 06-16-2010, 03:34 PM #7
Member
- Join Date
- Jun 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
JTree/TreeModelListener
By sQu3aKy in forum AWT / SwingReplies: 1Last Post: 04-28-2010, 07:47 AM -
JTree only display .txt
By collin389 in forum AWT / SwingReplies: 2Last Post: 12-04-2009, 09:50 PM -
Jtree Help
By miladirooni in forum New To JavaReplies: 1Last Post: 10-28-2009, 12:00 AM -
zip to JTree
By icsbcn in forum AWT / SwingReplies: 8Last Post: 09-01-2009, 04:26 PM -
Move JTree item to another JTree.
By Melki in forum AWT / SwingReplies: 8Last Post: 07-09-2009, 11:59 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks