This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Java's JTree (TreePath) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Java Watch "Java New topic

Java's JTree (TreePath)

Daniel Akesson

Joined: Jun 26, 2004
Posts: 6
Well, this is screwed up! Or at least it seems so.

Now here's an example:

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:


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
| | --
| | --
| | -- 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()!!

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299

Moving this to the Swing forum...

GenRocket - Experts at Building Test Data
A. Wolf
Ranch Hand

Joined: Sep 28, 2003
Posts: 57
im probably misunderstanding this since it seems too easy... but couldn't you just do


but if you want to add nodes to it even if it isn't selected why don't you just say

??? this all doesn't make much sense... to me... why do you want to add something to a node that is not selected? shouldn't an action be associated with a user event?
maybe you can supply more info about what this program is supposed to do and why?
Daniel Akesson

Joined: Jun 26, 2004
Posts: 6
Ahh, well... The tree is more dynamic and used than the user selecting in it! The tree shows whats on a ftp in a treestructure. Now, I gotta look into it more, but as I have it now I loop trough the tree for a match to a surtain path (i.e "/pub/iso/mandrake/") and when a math is found I return the responding TreePath. Apparently I cant recreate one in ANOTHER tree and just use that on this tree (which should probable go much faster).

Why this? Well, the path is added to a Vector (String) queue, so, when the user presses the "Download queue" button I need to get the TreePath out to use with adding a new node (tree), WITHOUT having anything to do what the user is doing.. (selected). That is, this downloadthread is working by itself while the user can do something else (more or less). Specially continue to explore the ftp (just see file size and all that that is saved from first exploration), and not be bothered by the downloading.

Hmm, now when I think about it.. maybe I should "save" the TreePath obj instead... hmm.. oh well. This works fine.
wood burning stoves
subject: Java's JTree (TreePath)
Similar Threads
JTree expand node and highlight
Select a node in Jtree
Subtree in JTree
JTree not updating