java - How to create a loop which will check if Jtree contains a value from Set <string> and reselects them automatically after a button press? -
how create loop check if jtree
contains elements set <string>
, reselect them automatically after press of button.
i store info this:
testnos = (currenttestlabel.gettext() + classlabel.gettext()); myset1.add(testnos);
i've been struggling days, , wasn't able find info on or how achieve that. great.
you can create method return matching nodes. can iterating on nodes in tree , check if there names matches 1 in set.
public java.util.list<treepath> find(defaultmutabletreenode root, set<string> s) { java.util.list<treepath> paths = new arraylist<>(); @suppresswarnings("unchecked") enumeration<defaultmutabletreenode> e = root.depthfirstenumeration(); while (e.hasmoreelements()) { defaultmutabletreenode node = e.nextelement(); if (s.contains(node.tostring())) { paths.add(new treepath(node.getpath())); } } return paths; }
you can call method passing tree node , set of strings. please note need cast root
defaultmutabletreenode
because getroot
returns object.
java.util.list<treepath> treepaths= find((defaultmutabletreenode)tree.getmodel().getroot(), someset);
then iterate on treepaths , invoke removeselectionpath
deselect nodes
(treepath treepath : treepaths) { tree.getselectionmodel().removeselectionpath(treepath); }
Comments
Post a Comment