how to default select items in a tree

Hi All

I was wondering how to select by default certain items. If I loop through my items like

final Tree tree = new Tree(.....) ;
Iterator it = tree.getItemIds().iterator() ;
while( it.hasNext()) {
	Item itm = tree.getItem( ) ;
	System.out.println("item title is " + itm.getItemProperty("title")) ;;

In the above code I tried to have all items to be selected by default. But [b]

[/b] doesn’t select anything!

Any suggestions how to select an item ?


Just a couple of checks - is your tree is allowed to do multi selects and be immediate ?

Yes, here are some snippets:

final Tree tree = new Tree(null, createHierarchicalContainer() ) ;
public HierarchicalContainer createHierarchicalContainer() {
   treeContainer = new HierarchicalContainer();
		treeContainer.addContainerProperty("title", String.class, null);
		treeContainer.addContainerProperty("id", Integer.class, null);

		Item item;
		int itemId = 0;
		for (int i = start; i < end; i++) {
			item = treeContainer.addItem(itemId);
			treeContainer.setChildrenAllowed(itemId, true);
			List<Category> cats = sources.get(i).getCategories();
			for (int j = 0; j < cats.size(); j++) {
				item = treeContainer.addItem(itemId);
				treeContainer.setParent(itemId, itemId - j -1);
				treeContainer.setChildrenAllowed(itemId, false);
		return treeContainer;
} takes an item ID, not an item, so you should use the value returned by the iterator.

On the other hand, to select all items, you could simply do tree.setValue(tree.getItemIds()). For a multi-selectable tree or table (or other AbstractSelect), the value is the selection as a Set, and setValue() should automatically copy the given Collection to a new set.

thnx for pointing that out!

I finally solved the problem by moving your code just before the statement where I add the tree to the layout object!