tags:

views:

9

answers:

1

I create a GWT Tree, and I would like it to be initially displayed with open nodes. Let's take the standard Tree example from GWT javadocs:

public class TreeExample implements EntryPoint {

  public void onModuleLoad() {
    // Create a tree with a few items in it.
    TreeItem root = new TreeItem("root");
    root.addItem("item0");
    root.addItem("item1");
    root.addItem("item2");

    // Add a CheckBox to the tree
    TreeItem item = new TreeItem(new CheckBox("item3"));
    root.addItem(item);

    Tree t = new Tree();
    t.addItem(root);

    // Add it to the root panel.
    RootPanel.get().add(t);
  }
}

I want it initially displayed as:

root
  item0
  item1
  item2
  item3

Now, I thought that it was as simple as setting the state of the TreeItem that I want to be opened by calling setState(true): javadoc for setState says "Sets whether this item's children are displayed". However, if I add for example

root.setState(true);

to the above example, I don't get the expected effect. Apparently nothing changes when I do root.setState(true); or root.setState(false);: the tree is always displayed with its nodes closed.

How do I get the desired behaviour?

+1  A: 

The call to setState() depends on the sequence of the method calls to TreeItem and Tree (as stated here).

As a rule of thumb call setState(true) after adding all the items to the TreeItem and after adding the root item to the Tree.

z00bs