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 ... ------------------
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
posted
0
Is there any sample code you have that you could share with me.
Vinod Venugopal
Ranch Hand
Joined: Dec 06, 2000
Posts: 148
posted
0
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;