Two Laptop Bag
The moose likes Swing / AWT / SWT and the fly likes Select a node in Jtree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Select a node in Jtree" Watch "Select a node in Jtree" New topic

Select a node in Jtree

Swetha Krish

Joined: Jun 27, 2011
Posts: 6
I have a text box and when the user enters the value and click the "Search Button", the value needs to be searched in the Jtree and need to highlight the node.

If I enter the string and hit search , I am able to search the tree and get the exact node and its path.

But I could not make it selected. If the node is collapsed I need to expand and select it.

Path count is 3.

Can anyone help in getting this done.
Arun Chidam
Ranch Hand

Joined: May 08, 2006
Posts: 79
Can you please try the below bit of code....

SCJP 1.4, SCWCD 1.4
Swetha Krish

Joined: Jun 27, 2011
Posts: 6
I tried the code and still the given node is not selected.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Once again I must be missing something. Your code

creates a brand new JTree that has nothing to do with the JTree that is displaying your TreeModel. I would expect you to use the JTree that is currently displaying the TreeModel.

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46367
. . . and welcome to the Ranch Swetha Krish
Swetha Krish

Joined: Jun 27, 2011
Posts: 6
I got it.I worked...Thank you....
Swetha Krish

Joined: Jun 27, 2011
Posts: 6
Is it possible to return all the possible nodes matching the search string and make them selected.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

I don't see why not. Of course you would have to change the tree's selection model to allow multiple selections, but beyond that it's probably a very simple change to your existing code.
Darryl Burke

Joined: May 03, 2008
Posts: 5115

Continued here:

luck, db
There are no new questions, but there may be new answers.
I agree. Here's the link:
subject: Select a node in Jtree
It's not a secret anymore!