• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Searching JTree

 
Venkatesh Raman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Venkatesh Raman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any sample code you have that you could share with me.
 
Vinod Venugopal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nodecaught is of type String, storing value of text entered in textfield
 
Venkatesh Raman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vinod,
Thanks a lot. The code is working and I appreciate your time to help me out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic