This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Searching a JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Searching a JTree" Watch "Searching a JTree" New topic
Author

Searching a JTree

kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
New task ... !
I have this tree, whose nodes I create from a database. As I read in the details for each node, I create the node, add it to the Tree, and enter it into a hashMap. For each subsequent node, then , I can check that it hasn't already been created. That all works fine. The HashMap simply maps the String represneting the node's name as displayed on the tree, to the Node itself.
Now, I want to allow the user to type in the name of a node, press a button, and the tree will show that node selected.
The problem is that as I have written it so far, I can get at the Node itself from the hashMap, but to setSelectionPath or setSelectionRow for the tree, I need to know either the node's Path or what Row it is in - neither of which are in the hashMap.
I've tried keeping a track of which row a node is in, but it's long-winded cos it has to react to every expand and collapse of the tree.
so, what I could do with is a method whereby, given a node, (and knowing that this node exists in the tree)I can find it's TreePath. Is this possible / easy ?
Many thanks,
Kate

kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
OK, solved that. the Node's getPath() method returns an array of nodes, but I can construct a TrePath from that array.
Next, just need to get the scrollPane to scroll down to the selected Path, and it will be great !
Kate
George Thomas
Ranch Hand

Joined: Oct 06, 2000
Posts: 35
Kate,
Does getPath() return an array of nodes under a single parent but a "true" path from top to bottom?
(I'm working on a similar function where I need to return a tree and go to a selected node. )

George.
kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
Hi George,
as I understand it, getPath() returns an array of Nodes which define a path from top to bottom, not an array of Children of a partcular node.
e.g. my myNode.getPath() gave me
{ theRoot, aFirstLevelNode, myNode}
Hence you can construct the path using that array :
TreePath(myNode.getPath()) gives you the unique Path to that Node.
If you want all the children under a Node you can get them - it's another method called something obvious like getChildNodes().
Does that help ?
Kate
George Thomas
Ranch Hand

Joined: Oct 06, 2000
Posts: 35
Kate,
Many thanks! I can't tell you how much this has helped. (It turns the users on to see that tree "open up" like magic before their very eyes.)

George.
kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
George (or anyone....)
So, I have this nice long tree, and users can find any node within it, whether expanded or not. However, the tree is long, so you have to scroll down to see the last ones.
If the user searches for, and finds, a node which is off the screen, I'd like the tree to scrollitself down so their node is on view.
Any ideas ?
Kate
kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
don't worry : it was

Now, is it easy to collapse all branches of a tree, or will I have to write something ?
Kate
George Thomas
Ranch Hand

Joined: Oct 06, 2000
Posts: 35
Kate,
My understanding is that you collapse the branches by referencing the row in the JTree object. In my find function I wanted to collapse the previously selected found node before I expanded the tree to the next found node. I did the following:

Were trying to find the argument num in the tree. First the the last expansion is collapsed by finding the last selected row and then collapsing rows backwards to the root.
Hope this helps.
George.
George Thomas
Ranch Hand

Joined: Oct 06, 2000
Posts: 35
Kate,
Sorry the following code was in there while I was trying to get the collapse to work:

It doesn't work, its the collapsing of rows in reverse that works. We call it the "reverse-row-collapse-thing".
Peter Deschamps
Greenhorn

Joined: Aug 04, 2001
Posts: 5
I have this tree, whose nodes I create from a database. As I read in the details for each node, I create the node, add it to the Tree, and enter it into a hashMap.
>>>>
Hi, I have a very similar business problem. I use an Oracle 8i
database stored procedure to return a cursor containing my list
of nodes and their properties.
1. I populate all the nodes into the JTree first right under the ROOT level.
2. then I make a second pass of the tree and move all the nodes under their parent nodes.
I have to make the second pass since I cannot add a node to a tree under a parent if the parent node doesn't yet exist (i.e. parent is farther down in my list). Yes, this second pass gets longer and longer as I move each child and have to search for its parent based on a Child's UserObject property (String parentnodeid).
This all works fine. I thought about perhaps using the HashMap idea but didn't (yet...). My JTree has about 1000 nodes so it isn't a trivial tree. The database retrieval using the cursor is 3 to 4 times as slow as the 1&2 pass tree populating routine so performance was not that big a deal.
Now here's the problem/issue and my design has me concerned:
I need to add 'searching' capabilities, expand/collapse, auto-scroll, just like you described. Since your Tree code seems to do EXACTLY what I need, any chance you could share it? I'd be most grateful.
Peter
PS.
Also I need a standard FIND, FINDNEXT java object that receives user input to search for, then I'd call my JTree search function.
I tried searching google, sun and the like but can't find source code for this very common object. BTW, I am using JBuilder 4 IDE.
kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
Hi,
The current project has largely overwritten the code for my previous version and it's a bit of hassle to get it all up and running again, but I'll try to explain what I did. I think the key to it was to register each node in a hashmap, as it was created. The key used in the hashmap was the String to be displayed on the Node. So, if a Node exists, it's String will be in the HashMap, then to implement a Find, all you have to do is look it up in the HashMap. We believe that this method will generalise up, so that I can write something to implement a regular expression Find. Alas, other things have become more important, and I haven't done this yet, so this is a n exact match only search.
Here's the code from the ActionPerformed() method as triggered by the Find button :

btw the "dualExists" method can be understood as 'exists' it's just that in practice, I wrote a dualKeyable Hashmap class which was needed for other reasons.
Hope this helps
Kate
Peter Deschamps
Greenhorn

Joined: Aug 04, 2001
Posts: 5
Thanks Kate. I appreciate your assistance! I know wripping out code snippets can be a pain.
thanks again,
P
 
wood burning stoves
 
subject: Searching a JTree
 
Similar Threads
JTree row selection by mouseclick question
Hibernate Query question, entity beans..
Tree structure retrieval from Database
Using Custom Tree Cell rendering
Proper way of resizing JTree cells on changing node text