Google Web Toolkit (Beta)
Search this site:

# Class Tree

public class Tree
extends Widget
implements HasWidgets, SourcesTreeEvents, HasFocus
A standard hierarchical tree widget. The tree contains a hierarchy of TreeItems that the user can open, close, and select.

### CSS Style Rules

- .gwt-Tree { the tree itself }
- .gwt-Tree .gwt-TreeItem { a tree item }
- .gwt-Tree .gwt-TreeItem-selected { a selected tree item }

### 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

Tree() | Constructs an empty tree.
Tree(TreeImages) | Constructs a tree that uses the specified image bundle for images.

## Methods

add(Widget) | Adds the widget as a root tree item.
addFocusListener(FocusListener) | Adds a listener interface to receive mouse events.
addItem(String) | Adds a simple tree item containing the specified text.
addItem(TreeItem) | Adds an item to the root level of this tree.
addItem(Widget) | Adds a new tree item containing the specified widget.
addKeyboardListener(KeyboardListener) | Adds a listener interface to receive keyboard events.
addMouseListener(MouseListener) |
addTreeListener(TreeListener) | Adds a listener interface to receive tree events.
clear() | Clears all tree items from the current tree.
ensureSelectedItemVisible() | Ensures that the currently-selected item is visible, opening its parents and scrolling the tree as necessary.
getImageBase() | Gets this tree's default image package.
getItem(int) | Gets the top-level tree item at the specified index.
getItemCount() | Gets the number of items contained at the root of this tree.
getSelectedItem() | Gets the currently selected item.
getTabIndex() | Gets the widget's position in the tab index.
isKeyboardNavigationEnabled(TreeItem) | Indicates if keyboard navigation is enabled for the Tree and for a given TreeItem.
iterator() | Returns all `TreeItem.ContentPanel` elements contained within this tree.
onAttach() | This method is called when a widget is attached to the browser's document.
onBrowserEvent(Event) |
onDetach() | This method is called when a widget is detached from the browser's document.
onLoad() | This method is called immediately after a widget becomes attached to the browser's document.
remove(Widget) | Removes a child widget.
removeFocusListener(FocusListener) | Removes a previously added listener interface.
removeItem(TreeItem) | Removes an item from the root level of this tree.
removeItems() | Removes all items from the root level of this tree.
removeKeyboardListener(KeyboardListener) | Removes a previously added listener interface.
removeTreeListener(TreeListener) | Removes a previously added listener interface.
setAccessKey(char) | Sets the widget's 'access key'.
setFocus(boolean) | Explicitly focus/unfocus this widget.
setImageBase(String) | Sets the base URL under which this tree will find its default images.
setSelectedItem(TreeItem) | Selects a specified item.
setSelectedItem(TreeItem, boolean) | Selects a specified item.
setTabIndex(int) | Sets the widget's position in the tab index.
treeItemIterator() | Iterator of tree items.

## Constructor Detail

### Tree

public Tree()
Constructs an empty tree.

### Tree

public Tree(TreeImages images)
Constructs a tree that uses the specified image bundle for images.

#### Parameters

images
  a bundle that provides tree specific images

## Method Detail

### add

public void add(Widget widget)
Adds the widget as a root tree item.

#### Parameters

widget
  widget to add.

#### See Also

Widget)

### addFocusListener

public void addFocusListener(FocusListener listener)
Adds a listener interface to receive mouse events.

#### Parameters

listener
  the listener interface to add

### addItem

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

#### Parameters

itemText
  the text of the item to be added

#### Return Value

the item that was added

### addItem

public void addItem(TreeItem item)
Adds an item to the root level of this tree.

#### Parameters

item
  the item to be added

### addItem

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

#### Parameters

widget
  the widget to be added

### addKeyboardListener

public void addKeyboardListener(KeyboardListener listener)
Adds a listener interface to receive keyboard events.

#### Parameters

listener
  the listener interface to add

### addMouseListener

public void addMouseListener(MouseListener listener)

#### Parameters

listener

### addTreeListener

public void addTreeListener(TreeListener listener)
Adds a listener interface to receive tree events.

#### Parameters

listener
  the listener interface to add

### clear

public void clear()
Clears all tree items from the current tree.

### ensureSelectedItemVisible

public void ensureSelectedItemVisible()
Ensures that the currently-selected item is visible, opening its parents and scrolling the tree as necessary.

### getImageBase

public String getImageBase()
Gets this tree's default image package.

#### Return Value

the tree's image package

#### See Also

setImageBase

### getItem

public TreeItem getItem(int index)
Gets the top-level tree item at the specified index.

#### Parameters

index
  the index to be retrieved

#### Return Value

the item at that index

### getItemCount

public int getItemCount()
Gets the number of items contained at the root of this tree.

#### Return Value

this tree's item count

### getSelectedItem

public TreeItem getSelectedItem()
Gets the currently selected item.

#### Return Value

the selected item

### getTabIndex

public int getTabIndex()
Gets the widget's position in the tab index.

#### Return Value

the widget's tab index

### isKeyboardNavigationEnabled

protected boolean isKeyboardNavigationEnabled(TreeItem currentItem)
Indicates if keyboard navigation is enabled for the Tree and for a given TreeItem. Subclasses of Tree can override this function to selectively enable or disable keyboard navigation.

#### Parameters

currentItem
  the currently selected TreeItem

#### Return Value

`true` if the Tree will response to arrow keys by changing the currently selected item

### iterator

public Iterator iterator()
Returns all `TreeItem.ContentPanel` elements contained within this tree. The existence of the `TreeItem.ContentPanel` class is an implementation detail that may or may not be preserved in future versions of Tree.

### onAttach

protected void onAttach()
This method is called when a widget is attached to the browser's document. To receive notification after a Widget has been added from the document, override the onLoad method.

Subclasses that override this method must call `super.onAttach()` to ensure that the Widget has been attached to the underlying Element.

### onBrowserEvent

public void onBrowserEvent(Event event)

#### Parameters

event

### onDetach

protected void onDetach()
This method is called when a widget is detached from the browser's document. To receive notification before a Widget is removed from the document, override the onUnload method.

Subclasses that override this method must call `super.onDetach()` to ensure that the Widget has been detached from the underlying Element. Failure to do so will result in application memeroy leaks due to circular references between DOM Elements and JavaScript objects.

### onLoad

protected void onLoad()
This method is called immediately after a widget becomes attached to the browser's document.

### remove

public boolean remove(Widget w)
Removes a child widget.

#### Parameters

w
  the widget to be removed

#### Return Value

`true` if the widget was present

### removeFocusListener

public void removeFocusListener(FocusListener listener)
Removes a previously added listener interface.

#### Parameters

listener
  the listener interface to remove

### removeItem

public void removeItem(TreeItem item)
Removes an item from the root level of this tree.

#### Parameters

item
  the item to be removed

### removeItems

public void removeItems()
Removes all items from the root level of this tree.

### removeKeyboardListener

public void removeKeyboardListener(KeyboardListener listener)
Removes a previously added listener interface.

#### Parameters

listener
  the listener interface to remove

### removeTreeListener

public void removeTreeListener(TreeListener listener)
Removes a previously added listener interface.

#### Parameters

listener
  the listener interface to remove

### setAccessKey

public void setAccessKey(char key)
Sets the widget's 'access key'. This key is used (in conjunction with a browser-specific modifier key) to automatically focus the widget.

#### Parameters

key
  the widget's access key

### setFocus

public void setFocus(boolean focused)
Explicitly focus/unfocus this widget. Only one widget can have focus at a time, and the widget that does will receive all keyboard events.

#### Parameters

focused
  whether this widget should take focus or release it

### setImageBase

public void setImageBase(String baseUrl)
Sets the base URL under which this tree will find its default images. These images must be named "tree_white.gif", "tree_open.gif", and "tree_closed.gif".

#### Parameters

baseUrl

### setSelectedItem

public void setSelectedItem(TreeItem item)
Selects a specified item.

#### Parameters

item
  the item to be selected, or `null` to deselect all items

### setSelectedItem

public void setSelectedItem(TreeItem item, boolean fireEvents)
Selects a specified item.

#### Parameters

item
  the item to be selected, or `null` to deselect all items
fireEvents
  `true` to allow selection events to be fired

### setTabIndex

public void setTabIndex(int index)
Sets the widget's position in the tab index. If more than one widget has the same tab index, each such widget will receive focus in an arbitrary order. Setting the tab index to `-1` will cause this widget to be removed from the tab order.

#### Parameters

index
  the widget's tab index

### treeItemIterator

public Iterator treeItemIterator()
Iterator of tree items.