Getting a TreeNode on a JTree

  • Thread starter Allan Valeriano
  • Start date
A

Allan Valeriano

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.
 
A

Andrew Thompson

Allan said:
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.

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200707/1
 
A

Allan Valeriano

'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?"
 
A

Andrew Thompson

Allan said:
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.

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200707/1
 
A

Allan Valeriano

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,301
Latest member
BuyPureganics

Latest Threads

Top