Search code examples
javatreeviewswtzkitems

SWT Tree getItems()


I'm trying to get all items from a specific SWT tree (and create from them a zk Tree-- not important). the problem is that I can only get the first items of the Tree and i can't go more deep. i use tree.getItems() to get the first items but when I use item.getItems() I get an empty array();

swtTreeToZkTree(treeViewer.getTree().getItems(), zkTreeChildren);


public void swtTreeToZkTree(TreeItem[] treeItems, Treechildren treeChildren) {
    for (TreeItem item : treeItems) {
        Treeitem treeItem = new Treeitem();
        treeItem.setParent(treeChildren);
        Treerow treeRow = new Treerow();
        treeRow.setParent(treeItem);
        Treecell treeCell = new Treecell();
        treeCell.setParent(treeRow);
        treeCell.setLabel(item.getText());

        if (item.getItemCount() > 0) {
            Treechildren treeChildrenSub = new Treechildren();
            treeChildrenSub.setParent(treeItem);
            swtTreeToZkTree(item.getItems(), treeChildrenSub);
        }
    }
}

Solution

  • The TreeItem objects for child nodes are not created until that branch of the tree is expanded.

    If you don't want to expand the whole tree you would be better off using the ITreeContentProvider content provider of the tree and calling the getElements and getChildren methods.

    If you want to expand the whole tree call the TreeViewer expandAll() method.