|
Page 1 of 1 |
|
Posted: Sun, 27th Jun 2004 01:29 Post subject: Java's Trees (PathTree) |
|
 |
Well, this is screwed up! Or at least it seems so.
Now here's an example:
Code: |
DefaultMutableTreeNode rNode = new DefaultMutableTreeNode("/");
DefaultMutableTreeNode cNode = null;
DefaultTreeModel tModel = new DefaultTreeModel(rNode);
DefaultMutableTreeNode rNode2 = new DefaultMutableTreeNode("/");
DefaultMutableTreeNode cNode2 = null;
DefaultTreeModel tModel2 = new DefaultTreeModel(rNode2);
JTree jt = new JTree(tModel);
JTree jt2 = new JTree(tModel2);
cNode = new DefaultMutableTreeNode("demo");
cNode2 = new DefaultMutableTreeNode("demo");
tModel.insertNodeInto(cNode, rNode, cNode.getChildCount());
tModel2.insertNodeInto(cNode2, rNode2, cNode2.getChildCount());
jt.expandRow(0);
jt2.expandRow(0);
TreePath tp = jt.getPathForRow(jt.getRowCount() - 1);
jt2.setSelectionPath(tp);
jScrollPaneDir.setViewportView(jt2);
...
|
Now, as you can see I got 2 exactly the same trees! The little test here shows that even if they have the same structure, I cant get the path from tree #1 and use that on tree #2!!! (??) I was wondering WHY? and how this can be easly solved! The reason I wanna be able to do this is because three #1 is a more complex tree while the other (tree #2) is supposed to be used to be able to select some particual node in that complex #1 tree. I had in mind to then build a tempurary tree to get the TreePath out, since apparently:
jt.setSelectionPath(tp.pathByAddingChild(node);
aint working either!! Which should be damn much nicer (and faster)!
(but please feel free to make this version work! I could have missed something with some object)
Say I got this in tree#1:
/
| -- demo
| | -- file.zip
| | -- file2.zip
| | -- dir2
| -- {dir3}
where {dir3} indicates that dir3 is now selected. Now, if I wanted to add some node to dir2 there is problem. I couldnt really find anything usefull to help me out with that! I NEED the TreePath to that node (dir2). There really just are:
TreePath pathTree = jt.getSelectionPath();
But that assumes you have selected dir2! Annoying. So, I thought (which is WAY better than make java select it by.. say the setSelectionRow(int row); that I could create the path with the "demo" + adding the "dir2" node. Thus do the: jt.setSelectionPath(tp.pathByAddingChild(new DefaultMutableTreeNode("dir2");
But ofcource that isnt working.
Then I thought, I could recreate a new temptree with just the nodes needed. /demo/dir2 in this case and get the path from that (since I here can use the jttemp.getRowCount(); and select the last one (since its only "one" tree, where I wanna select the last child so to speak))
So, to do: TreePath tptemp = jt.getPathForRow(jt.getRowCount());
And voilá! Well, so I thought. Didnt work to use that tptemp for tree #1!
Got damn its frustrating!
Anyone here got time to help me out? Hope this all made sence.. Basicly, what it comes down to. How do I get a TreePath to /demo/dir2 !! that works so I can use that to insert nodes into that if the node itself isnt even selected!? I really wanna avoid using getSelectionPath()!!
Thanks,
Ace
|
|
Back to top |
|
 |
Hitman02
Banned
Posts: 709
Location: USA
|
Posted: Thu, 1st Jul 2004 00:29 Post subject: |
|
 |
well i took a java cource this is kind of advance for me, but ask here it is all about programming and java, i ask there all the time and i guarantie u will find a solution there.
http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi
|
|
Back to top |
|
 |
Page 1 of 1 |
All times are GMT + 1 Hour |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group
|
|
 |
|