• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Java's JTree (TreePath)

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Swing forum...
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic