• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Searching JTree

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
------------------
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vinod,
Thanks a lot. The code is working and I appreciate your time to help me out.
 
This is my favorite show. And this is my favorite tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic