Google Web Toolkit (Beta)
Search this site:

# Class TreeItem

public class TreeItem
extends UIObject
implements HasHTML
An item that can be contained within a Tree.

### Example

    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);
      }
    }

## Constructors

TreeItem() | Creates an empty tree item.
TreeItem(String) | Constructs a tree item with the given HTML.
TreeItem(Widget) | Constructs a tree item with the given `Widget`.

## Methods

addItem(String) | Adds a child tree item containing the specified text.
addItem(TreeItem) | Adds another item as a child to this one.
addItem(Widget) | Adds a child tree item containing the specified widget.
getChild(int) | Gets the child at the specified index.
getChildCount() | Gets the number of children contained in this item.
getChildIndex(TreeItem) | Gets the index of the specified child item.
getFocusableWidget() | Returns the widget, if any, that should be focused on if this TreeItem is selected.
getHTML() | Gets this object's contents as HTML.
getParentItem() | Gets this item's parent.
getState() | Gets whether this item's children are displayed.
getText() | Gets this object's text.
getTree() | Gets the tree that contains this item.
getUserObject() | Gets the user-defined object associated with this item.
getWidget() | Gets the `Widget` associated with this tree item.
isSelected() | Determines whether this item is currently selected.
remove() | Removes this item from its tree.
removeItem(TreeItem) | Removes one of this item's children.
removeItems() | Removes all of this item's children.
setHTML(String) | Sets this object's contents via HTML.
setSelected(boolean) | Selects or deselects this item.
setState(boolean) | Sets whether this item's children are displayed.
setState(boolean, boolean) | Sets whether this item's children are displayed.
setText(String) | Sets this object's text.
setUserObject(Object) | Sets the user-defined object associated with this item.
setWidget(Widget) | Sets the current widget.

## Constructor Detail

### TreeItem

public TreeItem()
Creates an empty tree item.

### TreeItem

public TreeItem(String html)
Constructs a tree item with the given HTML.

#### Parameters

html
  the item's HTML

### TreeItem

public TreeItem(Widget widget)
Constructs a tree item with the given `Widget`.

#### Parameters

widget
  the item's widget

## Method Detail

### addItem

public TreeItem addItem(String itemText)
Adds a child tree item containing the specified text.

#### Parameters

itemText
  the text to be added

#### Return Value

the item that was added

### addItem

public void addItem(TreeItem item)
Adds another item as a child to this one.

#### Parameters

item
  the item to be added

### addItem

public TreeItem addItem(Widget widget)
Adds a child tree item containing the specified widget.

#### Parameters

widget
  the widget to be added

#### Return Value

the item that was added

### getChild

public TreeItem getChild(int index)
Gets the child at the specified index.

#### Parameters

index
  the index to be retrieved

#### Return Value

the item at that index

### getChildCount

public int getChildCount()
Gets the number of children contained in this item.

#### Return Value

this item's child count.

### getChildIndex

public int getChildIndex(TreeItem child)
Gets the index of the specified child item.

#### Parameters

child
  the child item to be found

#### Return Value

the child's index, or `-1` if none is found

### getFocusableWidget

protected HasFocus getFocusableWidget()
Returns the widget, if any, that should be focused on if this TreeItem is selected.

#### Return Value

widget to be focused.

### getHTML

public String getHTML()
Gets this object's contents as HTML.

#### Return Value

the object's HTML

### getParentItem

public TreeItem getParentItem()
Gets this item's parent.

#### Return Value

the parent item

### getState

public boolean getState()
Gets whether this item's children are displayed.

#### Return Value

`true` if the item is open

### getText

public String getText()
Gets this object's text.

#### Return Value

the object's text

### getTree

public final Tree getTree()
Gets the tree that contains this item.

#### Return Value

the containing tree

### getUserObject

public Object getUserObject()
Gets the user-defined object associated with this item.

#### Return Value

the item's user-defined object

### getWidget

public Widget getWidget()
Gets the `Widget` associated with this tree item.

#### Return Value

the widget

### isSelected

public boolean isSelected()
Determines whether this item is currently selected.

#### Return Value

`true` if it is selected

### remove

public void remove()
Removes this item from its tree.

### removeItem

public void removeItem(TreeItem item)
Removes one of this item's children.

#### Parameters

item
  the item to be removed

### removeItems

public void removeItems()
Removes all of this item's children.

### setHTML

public void setHTML(String html)
Sets this object's contents via HTML. Use care when setting an object's HTML; it is an easy way to expose script-based security problems. Consider using setText(String) whenever possible.

#### Parameters

html
  the object's new HTML

### setSelected

public void setSelected(boolean selected)
Selects or deselects this item.

#### Parameters

selected
  `true` to select the item, `false` to deselect it

### setState

public void setState(boolean open)
Sets whether this item's children are displayed.

#### Parameters

open
  whether the item is open

### setState

public void setState(boolean open, boolean fireEvents)
Sets whether this item's children are displayed.

#### Parameters

open
  whether the item is open
fireEvents
  `true` to allow open/close events to be fired

### setText

public void setText(String text)
Sets this object's text.

#### Parameters

text
  the object's new text

### setUserObject

public void setUserObject(Object userObj)
Sets the user-defined object associated with this item.

#### Parameters

userObj
  the item's user-defined object

### setWidget

public void setWidget(Widget widget)
Sets the current widget. Any existing child widget will be removed.

#### Parameters

widget
  Widget to set