Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

Getting a TreeNode on a JTree

Thread view: 
Allan Valeriano - 04 Jul 2007 20:17 GMT
How can I find a specific node on a JTree, having the node's user
object? Is there a way to do that without searching the tree with a
loop?

In example:

Considering that I have a tree where every node has on its userObject,
a String, I need something like:

DefaultMutableTreeNode node = tree.getNode("ABC");

I know the method getNode(Object userObject) doesn't exist, but what
I'd like to know is if is there any other method that do something
like that for me...
I could search the entire tree looking on each node's userObject to
know if it is the node I want do deal with, but this would be veeeery
bad considering that the tree could get very long.
Andrew Thompson - 04 Jul 2007 23:02 GMT
>How can I find a specific node on a JTree, having the node's user
>object? Is there a way to do that without searching the tree with a
>loop?

'Sure'.  Just have a tree full of one object, and
always return that...

>I could search the entire tree looking on each node's userObject to
>know if it is the node I want do deal with, but this would be veeeery
>bad considering that the tree could get very long.

It would probably be just as bad as trying to foist
such a long structure into a JTree and onto screen
in the first place.  OTOH if it is small enough for the
end user to handle and navigate, I am confident it is
small enough for the JRE to search.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Allan Valeriano - 05 Jul 2007 17:42 GMT
> 'Sure'.  Just have a tree full of one object, and
> always return that...

Sorry, I didn't get it. What did you mean?

In my case, I have a tree where all the nodes have only one king of
userObject.
I.e. Every single node represents a student, and the node has on its
userObject, the name of the student he represents. I want to find
John, for example, without having to search the entire tree, asking
for each node "Are you representing John?"
Andrew Thompson - 06 Jul 2007 09:06 GMT
>> 'Sure'.  Just have a tree full of one object, and
>> always return that...
>
>Sorry, I didn't get it. What did you mean?

My bad.  That was (dripping with) sarcasm.  

I obviously failed to make that point due to my
abysmal communication skills.  Maybe a wink
[ ;-) ] at the end would have helped get across
that I was being somewhat less than technically
helpful?

>In my case, I have a tree where all the nodes have only one king of
>userObject.
>I.e. Every single node represents a student, and the node has on its
>userObject, the name of the student he represents. I want to find
>John, for example, without having to search the entire tree, asking
>for each node "Are you representing John?"

Ok.. but I will bring you back to..

>I could search the entire tree looking on each node's userObject to
>know if it is the node I want do deal with, but this would be veeeery
>bad ..

'Over Optimization'.  Do you have code that supports
this claim?  Or are you simply over optimizing, trying
to solve a problem that does not exist?

>..considering that the tree could get very long.

How long is 'very long'?  A JTree with 40,000 nodes
would be (I guess) unmanagably large for this end-user
to usefully navigate, but I suspect the JRE could find
entries within it, relatively quickly.  That though led
me to suggest..

"It would probably be just as bad as trying to foist
such a long structure into a JTree and onto screen
in the first place.  OTOH if it is small enough for the
end user to handle and navigate, I am confident it is
small enough for the JRE to search."

So - noting that I have also failed to provide any
code to prove my point, not that I am interested
enough to write code proving anything - I feel if this
matter is of such interest to you, write some code
that supports your initial assertion that a 'very long'
(but still managable) JTree represents 'enough data
to display, but too much to search', and I might be
able to invest more thought in it.

Notes:
1) I do not know of any method that provides what
you want directly, and suspect if one exists, it would
simply be a 'convenience method' for searching
iteratively - in any case.
2) You might also store each node in a map using the
potential strings as keys, though I have not thought so
much about that yet, ..convince me it is a real problem
and I might think some more on it.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Allan Valeriano - 06 Jul 2007 12:44 GMT
> 2) You might also store each node in a map using the
> potential strings as keys,

As I didn't find any other way, I decided to map the tree. I think it
will work as I expected. I hope it doesn't waste too much memory on
the client side (considering that it's not a String tree, and the
nodes carry lots of information with them).
Thanks anyway


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.