This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Searching JTree  RSS feed

 
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 ...
------------------
 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!