- Type Parameters:
- T- The type of the- valueproperty within TreeItem.
- All Implemented Interfaces:
- EventTarget
- Direct Known Subclasses:
- CheckBoxTreeItem
TreeView. The model may be implemented such that values may be loaded in
 memory as they are needed.
 
 The model allows registration of listeners which will be notified as the
 number of items changes, their position or if the values themselves change.
 Note however that a TreeItem is not a Node, and therefore no visual
 events will be fired on the TreeItem. To get these events, it is necessary to
 add relevant observers to the TreeCell instances (via a custom cell factory -
 see the Cell class documentation for more details).
 
In the simplest case, TreeItem instances may be created in memory as such:
 TreeItem<String> root = new TreeItem<String>("Root Node");
 root.setExpanded(true);
 root.getChildren().addAll(
     new TreeItem<String>("Item 1"),
     new TreeItem<String>("Item 2"),
     new TreeItem<String>("Item 3")
 );
 TreeView<String> treeView = new TreeView<String>(root);
 
  private TreeView buildFileSystemBrowser() {
      TreeItem<File> root = createNode(new File("/"));
      return new TreeView<File>(root);
  }
  // This method creates a TreeItem to represent the given File. It does this
  // by overriding the TreeItem.getChildren() and TreeItem.isLeaf() methods
  // anonymously, but this could be better abstracted by creating a
  // 'FileTreeItem' subclass of TreeItem. However, this is left as an exercise
  // for the reader.
  private TreeItem<File> createNode(final File f) {
      return new TreeItem<File>(f) {
          // We cache whether the File is a leaf or not. A File is a leaf if
          // it is not a directory and does not have any files contained within
          // it. We cache this as isLeaf() is called often, and doing the
          // actual check on File is expensive.
          private boolean isLeaf;
          // We do the children and leaf testing only once, and then set these
          // booleans to false so that we do not check again during this
          // run. A more complete implementation may need to handle more
          // dynamic file system situations (such as where a folder has files
          // added after the TreeView is shown). Again, this is left as an
          // exercise for the reader.
          private boolean isFirstTimeChildren = true;
          private boolean isFirstTimeLeaf = true;
          @Override public ObservableList<TreeItem<File>> getChildren() {
              if (isFirstTimeChildren) {
                  isFirstTimeChildren = false;
                  // First getChildren() call, so we actually go off and
                  // determine the children of the File contained in this TreeItem.
                  super.getChildren().setAll(buildChildren(this));
              }
              return super.getChildren();
          }
          @Override public boolean isLeaf() {
              if (isFirstTimeLeaf) {
                  isFirstTimeLeaf = false;
                  File f = (File) getValue();
                  isLeaf = f.isFile();
              }
              return isLeaf;
          }
          private ObservableList<TreeItem<File>> buildChildren(TreeItem<File> TreeItem) {
              File f = TreeItem.getValue();
              if (f != null && f.isDirectory()) {
                  File[] files = f.listFiles();
                  if (files != null) {
                      ObservableList<TreeItem<File>> children = FXCollections.observableArrayList();
                      for (File childFile : files) {
                          children.add(createNode(childFile));
                      }
                      return children;
                  }
              }
              return FXCollections.emptyObservableList();
          }
      };
  }TreeItem supports the same event bubbling concept as elsewhere in the
 scenegraph. This means that it is not necessary to listen for events on all
 TreeItems (and this is certainly not encouraged!). A better, and far more low
 cost solution is to instead attach event listeners to the TreeView
 root item. As long as there is a path between
 where the event occurs and the root TreeItem, the event will be bubbled to the
 root item.
 
It is important to note however that a TreeItem is not a
 Node, which means that only the event types defined in TreeItem will be
 delivered. To listen to general events (for example mouse interactions), it is
 necessary to add the necessary listeners to the cells contained
 within the TreeView (by providing a cell factory).
 
The TreeItem class defines a number of events, with a defined hierarchy. These are shown below (follow the links to learn more about each event type):
The indentation shown above signifies the relationship between event types.
 For example, all TreeItem event types have
 treeNotificationEvent() as their
 parent event type, and the branch
 expand /
 collapse event types are both
 treeNotificationEvent(). For
 performance reasons, it is encouraged to listen
 to only the events you need to listen to. This means that it is encouraged
 that it is better to listen to, for example,
 TreeItem.valueChangedEvent(),
 rather than TreeItem.treeNotificationEvent().
- Since:
- JavaFX 2.0
- See Also:
- 
Property SummaryPropertiesTypePropertyDescriptionfinal BooleanPropertyThe expanded state of this TreeItem.final ObjectProperty<Node>The node that is generally shown to the left of the value property.final ReadOnlyBooleanPropertyRepresents the TreeItem leaf property, which is true if the TreeItem has no children.final ReadOnlyObjectProperty<TreeItem<T>>A property that represents the parent of this TreeItem.final ObjectProperty<T>A property representing the application-specific data contained within this TreeItem.
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic classAnEventthat contains relevant information for all forms of TreeItem modifications.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescription<E extends Event>
 voidaddEventFilter(EventType<E> eventType, EventHandler<? super E> eventHandler) Registers an event filter for this target.<E extends Event>
 voidaddEventHandler(EventType<E> eventType, EventHandler<? super E> eventHandler) Registers an event handler for this target.static <T> EventType<TreeItem.TreeModificationEvent<T>>An EventType used when the TreeItem receives a modification to its expanded property, such that the TreeItem is now in the collapsed state.static <T> EventType<TreeItem.TreeModificationEvent<T>>An EventType used when the TreeItem receives a modification to its expanded property, such that the TreeItem is now in the expanded state.Construct an event dispatch chain for this target.static <T> EventType<TreeItem.TreeModificationEvent<T>>An EventType used when the TreeItem receives a direct modification to its children list.static <T> EventType<TreeItem.TreeModificationEvent<T>>The general EventType used when the TreeItem receives a modification that results in the number of children being visible changes.final BooleanPropertyThe expanded state of this TreeItem.The children of this TreeItem.final NodeReturns the node that is generally shown to the left of the value property.The parent of this TreeItem.final TgetValue()Returns the application-specific data represented by this TreeItem.static <T> EventType<TreeItem.TreeModificationEvent<T>>An EventType used when the TreeItem receives a modification to its graphic property.final ObjectProperty<Node>The node that is generally shown to the left of the value property.final booleanReturns the expanded state of this TreeItem.booleanisLeaf()A TreeItem is a leaf if it has no children.final ReadOnlyBooleanPropertyRepresents the TreeItem leaf property, which is true if the TreeItem has no children.Returns the next sibling of the TreeItem.nextSibling(TreeItem<T> afterNode) Returns the next sibling after the given node.final ReadOnlyObjectProperty<TreeItem<T>>A property that represents the parent of this TreeItem.Returns the previous sibling of the TreeItem.previousSibling(TreeItem<T> beforeNode) Returns the previous sibling after the given node.final voidsetExpanded(boolean value) Sets the expanded state of this TreeItem.final voidsetGraphic(Node value) Sets the node that is generally shown to the left of the value property.final voidSets the application-specific data represented by this TreeItem.toString()Returns a string representation of thisTreeItemobject.static <T> EventType<TreeItem.TreeModificationEvent<T>>The base EventType used to indicate that an event has occurred within a TreeItem.static <T> EventType<TreeItem.TreeModificationEvent<T>>An EventType used when the TreeItem receives a modification to its value property.final ObjectProperty<T>A property representing the application-specific data contained within this TreeItem.Methods declared in class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods declared in interface javafx.event.EventTargetremoveEventFilter, removeEventHandler
- 
Property Details- 
valueA property representing the application-specific data contained within this TreeItem.- See Also:
 
- 
graphicThe node that is generally shown to the left of the value property. For best effect, this tends to be a 16x16 image.- See Also:
 
- 
expandedThe expanded state of this TreeItem.
- 
leafRepresents the TreeItem leaf property, which is true if the TreeItem has no children.- See Also:
 
- 
parentA property that represents the parent of this TreeItem.- See Also:
 
 
- 
- 
Constructor Details- 
TreeItempublic TreeItem()Creates an empty TreeItem.
- 
TreeItemCreates a TreeItem with the value property set to the provided object.- Parameters:
- value- The object to be stored as the value of this TreeItem.
 
- 
TreeItemCreates a TreeItem with the value property set to the provided object, and the graphic set to the provided Node.- Parameters:
- value- The object to be stored as the value of this TreeItem.
- graphic- The Node to show in the TreeView next to this TreeItem.
 
 
- 
- 
Method Details- 
treeNotificationEventThe base EventType used to indicate that an event has occurred within a TreeItem. When an event occurs in a TreeItem, the event is fired to any listeners on the TreeItem that the event occurs, before it 'bubbles' up the TreeItem chain by following the TreeItem parent property. This repeats until a TreeItem whose parent TreeItem is null is reached At this point the event stops 'bubbling' and goes no further. This means that events that occur on a TreeItem can be relatively cheap, as a listener needs only be installed on the TreeView root node to be alerted of events happening at any point in the tree.- Type Parameters:
- T- The type of the value contained within the TreeItem.
- Returns:
- the base EventType when an event has occurred within a TreeItem
 
- 
expandedItemCountChangeEventThe general EventType used when the TreeItem receives a modification that results in the number of children being visible changes. This is normally achieved via one of the sub-types of this EventType (seebranchExpandedEvent(),branchCollapsedEvent()andchildrenModificationEvent()for the three sub-types).- Type Parameters:
- T- The type of the value contained within the TreeItem.
- Returns:
- The general EventType when the TreeItem receives a modification
- Since:
- JavaFX 8.0
 
- 
branchExpandedEventAn EventType used when the TreeItem receives a modification to its expanded property, such that the TreeItem is now in the expanded state.- Type Parameters:
- T- The type of the value contained within the TreeItem.
- Returns:
- The EventType used when the TreeItem receives a modification
 
- 
branchCollapsedEventAn EventType used when the TreeItem receives a modification to its expanded property, such that the TreeItem is now in the collapsed state.- Type Parameters:
- T- The type of the value contained within the TreeItem.
- Returns:
- The EventType when the TreeItem receives a modification
 
- 
childrenModificationEventAn EventType used when the TreeItem receives a direct modification to its children list.- Type Parameters:
- T- The type of the value contained within the TreeItem.
- Returns:
- The EventType when the TreeItem receives a direct modification to its children list
 
- 
valueChangedEventAn EventType used when the TreeItem receives a modification to its value property.- Type Parameters:
- T- The type of the value contained within the TreeItem.
- Returns:
- The EventType when the TreeItem receives a modification to its value property
 
- 
graphicChangedEventAn EventType used when the TreeItem receives a modification to its graphic property.- Type Parameters:
- T- The type of the value contained within the TreeItem.
- Returns:
- The EventType when the TreeItem receives a modification to its graphic property
 
- 
setValueSets the application-specific data represented by this TreeItem.- Parameters:
- value- the application-specific data
 
- 
getValueReturns the application-specific data represented by this TreeItem.- Returns:
- the data represented by this TreeItem
 
- 
valuePropertyA property representing the application-specific data contained within this TreeItem.- Returns:
- the property representing the application-specific data contained within this TreeItem
- See Also:
 
- 
setGraphicSets the node that is generally shown to the left of the value property. For best effect, this tends to be a 16x16 image.- Parameters:
- value- The graphic node that will be displayed to the user.
 
- 
getGraphicReturns the node that is generally shown to the left of the value property. For best effect, this tends to be a 16x16 image.- Returns:
- The graphic node that will be displayed to the user.
 
- 
graphicPropertyThe node that is generally shown to the left of the value property. For best effect, this tends to be a 16x16 image.- Returns:
- The node that is generally shown to the left of the value property
- See Also:
 
- 
setExpandedpublic final void setExpanded(boolean value) Sets the expanded state of this TreeItem. This has no effect on a TreeItem with no children. On a TreeItem with children however, the result of toggling this property is that visually the children will either become visible or hidden, based on whether expanded is set to true or false.- Parameters:
- value- If this TreeItem has children, calling setExpanded with- truewill result in the children becoming visible. Calling setExpanded with- falsewill hide any children belonging to the TreeItem.
 
- 
isExpandedpublic final boolean isExpanded()Returns the expanded state of this TreeItem.- Returns:
- Returns the expanded state of this TreeItem.
 
- 
expandedPropertyThe expanded state of this TreeItem.- Returns:
- The expanded state property of this TreeItem
- See Also:
 
- 
isLeafpublic boolean isLeaf()A TreeItem is a leaf if it has no children. The isLeaf method may of course be overridden by subclasses to support alternate means of defining how a TreeItem may be a leaf, but the general premise is the same: a leaf can not be expanded by the user, and as such will not show a disclosure node or respond to expansion requests.- Returns:
- true if this TreeItem has no children
 
- 
leafPropertyRepresents the TreeItem leaf property, which is true if the TreeItem has no children.- Returns:
- the TreeItem leaf property
- See Also:
 
- 
getParentThe parent of this TreeItem. Each TreeItem can have no more than one parent. If a TreeItem has no parent, it represents a root in the tree model.- Returns:
- The parent of this TreeItem, or null if the TreeItem has no parent.
 
- 
parentPropertyA property that represents the parent of this TreeItem.- Returns:
- the parent property of this TreeItem
- See Also:
 
- 
getChildrenThe children of this TreeItem. This method is called frequently, and it is therefore recommended that the returned list be cached by any TreeItem implementations.- Returns:
- a list that contains the child TreeItems belonging to the TreeItem.
 
- 
previousSiblingReturns the previous sibling of the TreeItem. Ordering is based on the position of the TreeItem relative to its siblings in the children list belonging to the parent of the TreeItem.- Returns:
- A TreeItem that is the previous sibling of the current TreeItem, or null if no such sibling can be found.
 
- 
previousSiblingReturns the previous sibling after the given node. Ordering is based on the position of the given TreeItem relative to its siblings in the children list belonging to the parent of the TreeItem.- Parameters:
- beforeNode- The TreeItem for which the previous sibling is being sought.
- Returns:
- A TreeItem that is the previous sibling of the given TreeItem, or null if no such sibling can be found.
 
- 
nextSiblingReturns the next sibling of the TreeItem. Ordering is based on the position of the TreeItem relative to its siblings in the children list belonging to the parent of the TreeItem.- Returns:
- A TreeItem that is the next sibling of the current TreeItem, or null if no such sibling can be found.
 
- 
nextSiblingReturns the next sibling after the given node. Ordering is based on the position of the given TreeItem relative to its siblings in the children list belonging to the parent of the TreeItem.- Parameters:
- afterNode- The TreeItem for which the next sibling is being sought.
- Returns:
- A TreeItem that is the next sibling of the given TreeItem, or null if no such sibling can be found.
 
- 
toStringReturns a string representation of thisTreeItemobject.
- 
buildEventDispatchChainDescription copied from interface:EventTargetConstruct an event dispatch chain for this target. The event dispatch chain contains event dispatchers which might be interested in processing of events targeted at thisEventTarget. This event target is not automatically added to the chain, so if it wants to process events, it needs to add anEventDispatcherfor itself to the chain.In the case the event target is part of some hierarchy, the chain for it is usually built from event dispatchers collected from the root of the hierarchy to the event target. The event dispatch chain is constructed by modifications to the provided initial event dispatch chain. The returned chain should have the initial chain at its end so the dispatchers should be prepended to the initial chain. The caller shouldn't assume that the initial chain remains unchanged nor that the returned value will reference a different chain. - Specified by:
- buildEventDispatchChainin interface- EventTarget
- Parameters:
- tail- the initial chain to build from
- Returns:
- the resulting event dispatch chain for this target
 
- 
addEventHandlerpublic <E extends Event> void addEventHandler(EventType<E> eventType, EventHandler<? super E> eventHandler) Registers an event handler for this target.The handler is called when the target receives an Eventof the specified type during the bubbling phase of event delivery.The TreeItemclass allows registration of listeners which will be notified as the number of items changes, their position, or if the values themselves change. Note thatTreeItemis not aNode, and therefore no visual events will be fired on it. To get these events, it is necessary to add relevant observers to theTreeCellinstances via a custom cell factory (see theCellclass documentation for more details).- Specified by:
- addEventHandlerin interface- EventTarget
- Type Parameters:
- E- the event class of the handler
- Parameters:
- eventType- the type of the events received by the handler
- eventHandler- the event handler
 
- 
addEventFilterpublic <E extends Event> void addEventFilter(EventType<E> eventType, EventHandler<? super E> eventHandler) Registers an event filter for this target.The filter is called when the target receives an Eventof the specified type during the capturing phase of event delivery.The TreeItemclass allows registration of listeners which will be notified as the number of items changes, their position, or if the values themselves change. Note thatTreeItemis not aNode, and therefore no visual events will be fired on it. To get these events, it is necessary to add relevant observers to theTreeCellinstances via a custom cell factory (see theCellclass documentation for more details).- Specified by:
- addEventFilterin interface- EventTarget
- Type Parameters:
- E- the event class of the filter
- Parameters:
- eventType- the type of the events received by the filter
- eventHandler- the event filter
 
 
-