wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Searching 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 "Searching JTree" Watch "Searching JTree" New topic
Author

Searching JTree

Venkatesh Raman
Greenhorn

Joined: Aug 29, 2001
Posts: 8
Hi,
I am writing an application in which I have a tree with all the nodes created. I have a text field adn when the user enters a text and press the button, I should be able to go to that particular node. As I am new to Java, please help me ...
------------------
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
Get the root node, perform a breadth first search, get all the nodes in a Vector,then when Enter is pressed in the textfield, search for the particualr text in the Vector, if u get it ,get the position, get the node at that position in the vector, use tree path & set selection path, then highlight the node.
Vinod


- Vinod<br />-------<br />SCJP2
Venkatesh Raman
Greenhorn

Joined: Aug 29, 2001
Posts: 8
Is there any sample code you have that you could share with me.
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
I hope this helps..
textField.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ke)
{
String enteredText = textField.getText().trim();
//Change entered text to lower case
if(enteredText.equals(""))
return;
enteredText= enteredText.toLowerCase();
//
expandAllRows(); //optional
DefaultMutableTreeNode rootN = (DefaultMutableTreeNode)tree.getModel().getRoot();
Enumeration df=rootN.breadthFirstEnumeration();
String enumElem="";
Vector nodeVector=new Vector();
DefaultMutableTreeNode tempN=new DefaultMutableTreeNode();
while(df.hasMoreElements())
{
tempN=(DefaultMutableTreeNode)df.nextElement();
nodeVector.addElement(tempN);
}
int startSearchPos=0;
if(enteredText.equals(nodeCaught))
{
startSearchPos = nodePos+1;
}
else
startSearchPos = 0;
if(startSearchPos >= nodeVector.size())
startSearchPos=0;
nodeVector.remove(0);
// Start looking out for the text from startSearchPos
boolean foundFlag = false;
if(found)
foundFlag = true;
found=false;
for(int vectorIndex=startSearchPos;vectorIndex<nodeVector.size();vectorIndex++)>
{
String nodeString=nodeVector.elementAt(vectorIndex).toString();
if( (nodeString.toLowerCase().startsWith(enteredText.toLowerCase())) )
{
//match found
nodePos=vectorIndex;

DefaultMutableTreeNode tempNod=(DefaultMutableTreeNode)(nodeVector.elementAt(vectorIndex));
tree.scrollPathToVisible( new TreePath(tempNod.getPath()) );//scrollPathToVisible
tree.setSelectionPath( new TreePath(tempNod.getPath()) );

found = true;
break;
}
else
{
found=false;
}
}// end of for loop
nodeCaught = enteredText;
}

}
});
Venkatesh Raman
Greenhorn

Joined: Aug 29, 2001
Posts: 8
Hi Vinod,
Thanks a lot for sharing the sample code. Vinod, could you please clarify what is nodeCaught, and what type of variable it is?
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
nodecaught is of type String, storing value of text entered in textfield
Venkatesh Raman
Greenhorn

Joined: Aug 29, 2001
Posts: 8
Hi Vinod,
Thanks a lot. The code is working and I appreciate your time to help me out.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Searching JTree
 
Similar Threads
Java Mail Arabic Right to Left
HTML hyperlinks in SWT
Drag text field on label and set label's text in textfield
Icefaces - PanelGrid align
Problem in XmL Reading (argent... please have a look)