java - Get all TreeItems in an SWT Tree -
i want array of treeitems swt tree. however, method included in tree class, getitems()
returns items on first level of tree (i.e. aren't children of anything).
can suggest way of children/items?
the documentation of tree#getitems()
specific:
returns (possibly empty) array of items contained in receiver that direct item children of receiver. these roots of tree.
here sample code trick:
public static void main(string[] args) { display display = new display(); final shell shell = new shell(display); shell.settext("stackoverflow"); shell.setlayout(new filllayout()); final tree tree = new tree(shell, swt.multi); treeitem parentone = new treeitem(tree, swt.none); parentone.settext("parent 1"); treeitem parenttwo = new treeitem(tree, swt.none); parenttwo.settext("parent 2"); (int = 0; < 10; i++) { treeitem item = new treeitem(parentone, swt.none); item.settext(parentone.gettext() + " child " + i); item = new treeitem(parenttwo, swt.none); item.settext(parenttwo.gettext() + " child " + i); } parentone.setexpanded(true); parenttwo.setexpanded(true); list<treeitem> allitems = new arraylist<treeitem>(); getallitems(tree, allitems); system.out.println(allitems); shell.pack(); shell.open(); while (!shell.isdisposed()) { if (!display.readanddispatch()) display.sleep(); } display.dispose(); } private static void getallitems(tree tree, list<treeitem> allitems) { for(treeitem item : tree.getitems()) { getallitems(item, allitems); } } private static void getallitems(treeitem currentitem, list<treeitem> allitems) { treeitem[] children = currentitem.getitems(); for(int = 0; < children.length; i++) { allitems.add(children[i]); getallitems(children[i], allitems); } }
Comments
Post a Comment