Tree

Live Demo

The Tree component allows a natural way to represent data that has hierarchical relationships. The user can drill down in the hierarchy by expanding items by clicking on the expand arrow, and likewise collapse items. Tree is a selection component that allows selecting items. It also supports drag and drop, so you can drag items to and from a tree, and drop them in the hierarchy.

A typical use of the Tree component is for displaying a hierarchical menu, as illustrated in A Tree component as a menu, or for displaying file systems or hierarchical datasets.

tree example1

A Tree component as a menu

The data is managed in a container implementing the Hierarchical interface, such as HierarchicalContainer or FilesystemContainer. You can use ContainerHierarchicalWrapper to add hierarchical capability to any other container. Tree itself implements the interface and delegates operations to the underlying container.

Java

  1. // A menu tree
  2. Tree menu = new Tree();
  3. // Couple of childless root items
  4. menu.addItem("Mercury");
  5. menu.setChildrenAllowed("Mercury", false);
  6. menu.addItem("Venus");
  7. menu.setChildrenAllowed("Venus", false);
  8. // An item with hierarchy
  9. menu.addItem("Earth");
  10. menu.addItem("The Moon");
  11. menu.setChildrenAllowed("The Moon", false);
  12. menu.setParent("The Moon", "Earth");
  13. menu.expandItem("Earth"); // Expand programmatically
  14. ...

The result was shown in A Tree component as a menu in a practical situation, with the Tree wrapped inside a Panel. Tree itself does not have scrollbar, but Panel can be used for the purpose.

The caption of tree items is by default the item ID. You can define how the item captions are determined with setItemCaptionMode(), as explained “Selection Component Item Captions”.

Handling Selection and Clicks

Tree is a selection component, which are described in “Selection Components”. You can thereby get or set the currently selected item by the value property of the tree, that is, with getValue() and setValue(). When the user selects an item, the tree will receive an ValueChangeEvent, which you can catch with a ValueChangeListener.

Java

  1. // Handle selection changes
  2. menu.addValueChangeListener(event -> { // Java 8
  3. if (event.getProperty() != null &&
  4. event.getProperty().getValue() != null) {
  5. location.setValue("The cat is in " +
  6. event.getProperty().getValue());
  7. }
  8. });

Tree is selectable by default; you can disallow selection with setSelectable(false).

Tree also emits ItemClickEvents when items are clicked. This way you can handle item clicks also when selection is not enabled or you want special user interaction specifically on clicks.

Java

  1. tree.addItemClickListener(
  2. new ItemClickEvent.ItemClickListener() {
  3. public void itemClick(ItemClickEvent event) {
  4. // Pick only left mouse clicks
  5. if (event.getButton() == ItemClickEvent.BUTTON_LEFT)
  6. Notification.show("Left click",
  7. Notification.Type.HUMANIZED_MESSAGE);
  8. }
  9. });

Expanding and Collapsing Items

An item can have children only if the childrenAllowed property is set as true. The expand indicator is shown when and only when the property is true. The property is defined in the container and can be set with setChildrenAllowed().

Expanding an item fires an Tree.ExpandEvent and collapsing an Tree.CollapseEvent, which you can handle with respective listeners.

Java

  1. tree.addExpandListener(new Tree.ExpandListener() {
  2. public void nodeExpand(ExpandEvent event) {
  3. Notification.show("Expand!");
  4. }
  5. });

You can expand and collapse items programmatically with expandItem() or expandItemRecursively().

Java

  1. // Expand all items that can be
  2. for (Object itemId: tree.getItemIds())
  3. tree.expandItem(itemId);
Tip
Tree itself does not support lazy loading, which makes it impractical for huge hierarchies. You can implement one kind of lazy loading by adding items in an expand listener and removing them in a collapse listener. For more proper lazy loading, you can use TreeTable or hierarchical support extension for Grid.

CSS Style Rules

CSS

  1. .v-tree {}
  2. .v-tree-node {} /* A node (item) */
  3. .v-tree-node-caption {} /* Caption of the node */
  4. .v-tree-node-children {} /* Contains child nodes */
  5. .v-tree-node-root {} /* If node is a root node */
  6. .v-tree-node-leaf {} /* If node has no children */

Generating Item Styles

You can style each tree item individually by generating a style name for them with a Tree.ItemStyleGenerator, which you assign to a tree with setItemStyleGenerator(). The generator should return a style name for each item or null.

Java

  1. // Show all leaf nodes as disabled
  2. tree.setItemStyleGenerator(new Tree.ItemStyleGenerator() {
  3. @Override
  4. public String getStyle(Tree source, Object itemId) {
  5. if (! tree.hasChildren(itemId))
  6. return "disabled";
  7. return null;
  8. }
  9. });

The style names are prefixed with v-tree-node-caption-. You could thereby define the item styling as follows:

CSS

  1. .v-tree-node-caption-disabled {
  2. color: graytext;
  3. font-style: italic;
  4. }