java - Checking if JTree node exist using path -


i have classic jtree populated nods. lets assume tree looks this:

root |-fruit |--apple |--orange |-objects |--table |--car 

is there way in java check if node exists using assumed path this:

treenode found = model.getnodeornull("\\fruit\\apple") 

so if node in given location exists it's returned, if not null returned? there such mechanism in java?

you might experiment along these lines.

tree node location

example output

food:pizza found true od:pizza found false sports:hockey found true sports:hockey2 found false 

treenodelocation.java

import java.awt.borderlayout; import java.awt.event.*; import javax.swing.*; import javax.swing.text.position; import javax.swing.tree.treepath;  public class treenodelocation {      private jtree tree = new jtree();      treenodelocation() {         jpanel p = new jpanel(new borderlayout(2,2));          final jtextfield find = new jtextfield("food:pizza");         find.addactionlistener(new actionlistener() {             public void actionperformed(actionevent ae) {                 boolean found = findtext(find.gettext());                 system.out.println(find.gettext() + " found " + found);             }         });         p.add(find, borderlayout.page_start);          tree.setvisiblerowcount(8);         (int row=tree.getrowcount(); row>=0; row--) {             tree.expandrow(row);         }          p.add(new jscrollpane(tree),borderlayout.center);          joptionpane.showmessagedialog(null, p);     }      public boolean findtext(string nodes) {         string[] parts = nodes.split(":");         treepath path = null;         (string part : parts) {             int row = (path==null ? 0 : tree.getrowforpath(path));             path = tree.getnextmatch(part, row, position.bias.forward);             if (path==null) {                 return false;             }         }         tree.scrollpathtovisible(path);         tree.setselectionpath(path);          return path!=null;     }      public static void main(string[] args) {         swingutilities.invokelater(new runnable() {             @override             public void run() {                 new treenodelocation();             }         });     } } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -