File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Select a node in Jtree

Swetha Krish
Greenhorn

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
Greenhorn

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
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
. . . and welcome to the Ranch Swetha Krish
Swetha Krish
Greenhorn

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

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
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Continued here: http://www.coderanch.com/t/544262/GUI/java/Help-needed-selecting-multiple-nodes


luck, db
There are no new questions, but there may be new answers.
 
Don't get me started about those stupid light bulbs.
 
subject: Select a node in Jtree