value property within TreeItem.
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);
This approach works well for simple tree structures, or when the data is not
excessive (so that it can easily fit in memory). In situations where the size
of the tree structure is unknown (and therefore potentially huge), there is
the option of creating TreeItem instances on-demand in a memory-efficient way.
To demonstrate this, the code below creates a file system browser:
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 Events
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.,
rather than TreeItem..
TreeView
| Modifier and Type | Class and Description |
|---|---|
| public static class | TreeItem.
The TreeModificationEvent T>An |
| Modifier and Type | Field and Description |
|---|---|
| private static final EventType | |
| private static final EventType | |
| pack-priv ObservableList | |
| private static final EventType | |
| private ListChangeListener | |
| private final EventHandlerManager | |
| private BooleanProperty | |
| private static final EventType | |
| private int | |
| private boolean | |
| private ObjectProperty | |
| private static final EventType | |
| private boolean | |
| private final EventHandler | |
| pack-priv Comparator | |
| pack-priv TreeSortMode | |
| private ReadOnlyBooleanWrapper | |
| private ReadOnlyObjectWrapper | |
| private int | |
| pack-priv int | |
| private static final EventType | |
| private ObjectProperty | |
| private static final EventType |
| Access | Constructor and Description |
|---|---|
| public | |
| public | TreeItem(final T
The object to be stored as the value of this TreeItem. value)Creates a TreeItem with the value property set to the provided object. |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public <E extends Event> void | addEventFilter(EventType<E>
the type of the events received by the filter eventType, EventHandler<? super E> the event filter eventHandler)Overrides default javafx. Registers an event filter for this target. |
| public <E extends Event> void | addEventHandler(EventType<E>
the type of the events received by the handler eventType, EventHandler<? super E> the event handler eventHandler)Overrides default javafx. Registers an event handler for this target. |
| public static < The type of the value contained within the TreeItem. T> EventType | Returns: The EventType when the TreeItem receives a modificationAn EventType used when the TreeItem receives a modification to its expanded property, such that the TreeItem is now in the collapsed state. |
| public static < The type of the value contained within the TreeItem. T> EventType | Returns: The EventType used when the TreeItem receives a modificationAn EventType used when the TreeItem receives a modification to its expanded property, such that the TreeItem is now in the expanded state. |
| public EventDispatchChain | buildEventDispatchChain(EventDispatchChain
the initial chain to build from tail)Implements javafx. Construct an event dispatch chain for this target. |
| private void | |
| public static < The type of the value contained within the TreeItem. T> EventType | Returns: The EventType when the TreeItem receives a direct modification to its children listAn EventType used when the TreeItem receives a direct modification to its children list. |
| private void | |
| public static < The type of the value contained within the TreeItem. T> EventType | Returns: The general EventType when the TreeItem receives a modificationThe general EventType used when the TreeItem receives a modification that results in the number of children being visible changes. |
| public final BooleanProperty | Returns: The expanded state property of this TreeItemThe expanded state of this TreeItem. |
| private void | |
| public ObservableList | Returns: a list that contains the child TreeItems belonging to the TreeItem.The children of this TreeItem. |
| pack-priv int | |
| public final Node | Returns: The graphic node that will be displayed to the user.Returns the node that is generally shown to the left of the value property. |
| public final TreeItem | Returns: The parent of this TreeItem, or null if the TreeItem has no parent.The parent of this TreeItem. |
| private TreeItem | |
| public final T | Returns: the data represented by this TreeItemReturns the application-specific data represented by this TreeItem. |
| public static < The type of the value contained within the TreeItem. T> EventType | Returns: The EventType when the TreeItem receives a modification to its graphic propertyAn EventType used when the TreeItem receives a modification to its graphic property. |
| public final ObjectProperty | Returns: The node that is generally shown to the left of the value propertyThe node that is generally shown to the left of the value property. |
| public final boolean | Returns: Returns the expanded state of this TreeItem.Returns the expanded state of this TreeItem. |
| public boolean | |
| public final ReadOnlyBooleanProperty | Returns: the TreeItem leaf propertyRepresents the TreeItem leaf property, which is true if the TreeItem has no children. |
| public TreeItem | Returns: A TreeItem that is the next sibling of the current TreeItem, or null if no such sibling can be found.Returns the next sibling of the TreeItem. |
| public TreeItem | Returns: A TreeItem that is the next sibling of the given TreeItem, or null if no such sibling can be found.The TreeItem for which the next sibling is being
sought. afterNode)Returns the next sibling after the given node. |
| public final ReadOnlyObjectProperty | Returns: the parent property of this TreeItemA property that represents the parent of this TreeItem. |
| public TreeItem | Returns: A TreeItem that is the previous sibling of the current TreeItem, or null if no such sibling can be found.Returns the previous sibling of the TreeItem. |
| public TreeItem | Returns: A TreeItem that is the previous sibling of the given TreeItem, or null if no such sibling can be found.The TreeItem for which the previous sibling is being
sought. beforeNode)Returns the previous sibling after the given node. |
| public <E extends Event> void | removeEventFilter(EventType<E>
the event type from which to unregister eventType, EventHandler<? super E> the event filter eventHandler)Overrides default javafx. Unregisters a previously registered event filter from this target. |
| public <E extends Event> void | removeEventHandler(EventType<E>
the event type from which to unregister eventType, EventHandler<? super E> the event handler eventHandler)Overrides default javafx. Unregisters a previously registered event handler from this target. |
| private void | runSort(ObservableList<TreeItem<T>> children, Comparator<TreeItem<T>> comparator, TreeSortMode sortMode)
|
| public final void | setExpanded(boolean
If this TreeItem has children, calling setExpanded with
value)true will result in the children becoming visible.
Calling setExpanded with false will hide any children
belonging to the TreeItem.Sets the expanded state of this TreeItem. |
| public final void | setGraphic(Node
The graphic node that will be displayed to the user. value)Sets the node that is generally shown to the left of the value property. |
| private void | |
| private void | |
| public final void | setValue(T
the application-specific data value)Sets the application-specific data represented by this TreeItem. |
| pack-priv void | |
| private void | sort(final ObservableList<TreeItem<T>> children, final Comparator<TreeItem<T>> comparator, final TreeSortMode sortMode)
|
| public String | Returns: a string representation of thisTreeItem object.Overrides java. Returns a string representation of this |
| public static < The type of the value contained within the TreeItem. T> EventType | Returns: the base EventType when an event has occurred within a TreeItemThe base EventType used to indicate that an event has occurred within a TreeItem. |
| private void | |
| private static <T> void | |
| private void | |
| public static < The type of the value contained within the TreeItem. T> EventType | Returns: The EventType when the TreeItem receives a modification to its value propertyAn EventType used when the TreeItem receives a modification to its value property. |
| public final ObjectProperty | Returns: the property representing the application-specific data contained within this TreeItemA property representing the application-specific data contained within this TreeItem. |