Srijani Ghosh
Srijani Ghosh

Reputation: 4216

All the nodes in Tree not getting expanded

I am trying to learn TreeViewer in RCP. I wrote this small piece of code for that.

public class TreeViewClass extends ViewPart {

public static final String ID = "TreeViewerDemo.treeView";

public TreeViewClass() {
    // TODO Auto-generated constructor stub
}

@Override
public void createPartControl(Composite parent) {

    TreeViewer treeViewer = new TreeViewer(parent, SWT.BORDER);
    Tree tree = treeViewer.getTree();

    TreeItem root = new TreeItem(tree, 0);
    root.setText("Root Node");

    TreeItem childNode1 = new TreeItem(root, 0);
    childNode1.setText("Child Node 1");

    TreeItem childNode2 = new TreeItem(childNode1, 0);
    childNode2.setText("Child Node 2");
}

@Override
public void setFocus() {
    // TODO Auto-generated method stub

}

}

When I am running this code, The tree gets displayed, but only the root node. When I click on the root node(with a arrow icon, which means it has children), the node does not display the children nodes. Later I discovered that, when I apply setExpanded(true) to a node, it gets displayed, otherwise, it doesn't.

Where is the problem in my code?

Thanks!

Upvotes: 0

Views: 96

Answers (1)

greg-449
greg-449

Reputation: 111142

If you are using TreeViewer you should not use TreeItem. Instead you use a content provider and label provider. If you want to use TreeItem use Tree. Mixing the two will give unpredictable results.

When using TreeItem you need to call setExpanded(true) on each item you want to be expanded.

Upvotes: 1

Related Questions