TreeCell implementation that draws a
CheckBox node inside the cell, along with support for common
interactions (discussed in more depth shortly).
To make creating TreeViews with CheckBoxes easier, a convenience class
called CheckBoxTreeItem is provided. It is highly recommended
that developers use this class, rather than the regular TreeItem
class, when constructing their TreeView tree structures. Refer to the
CheckBoxTreeItem API documentation for an example on how these two classes
can be combined.
When used in a TreeView, the CheckBoxCell is rendered with a CheckBox to
the right of the 'disclosure node' (i.e. the arrow). The item stored in
CheckBoxTreeItem#getValue() will then have the StringConverter called
on it, and this text will take all remaining horizontal space. Additionally,
by using CheckBoxTreeItem, the TreeView will automatically handle
situations such as:
CheckBox beside an item that has children will
result in all children also becoming selected/unselected.
CheckBox beside an item that has a parent will
possibly toggle the state of the parent. For example, if you select a
single child, the parent will become indeterminate (indicating partial
selection of children). If you proceed to select all children, the
parent will then show that it too is selected. This is recursive, with
all parent nodes updating as expected.
CheckBoxTreeItem is not desirable,
then it is necessary to call one of the constructors where a Callback
is provided that can return an ObservableValue<Boolean>
given a TreeItem instance. This ObservableValue<Boolean>
should represent the boolean state of the given TreeItem.
Note that the CheckBoxTreeCell renders the CheckBox 'live', meaning that
the CheckBox is always interactive and can be directly toggled by the user.
This means that it is not necessary that the cell enter its
editing state (usually by the user double-clicking
on the cell). A side-effect of this is that the usual editing callbacks
(such as on edit commit)
will not be called. If you want to be notified of changes,
it is recommended to directly observe the boolean properties that are
manipulated by the CheckBox.
| Modifier and Type | Field and Description |
|---|---|
| private ObservableValue | |
| private final CheckBox | |
| private ObjectProperty | |
| private BooleanProperty | |
| private ObjectProperty |
| Access | Constructor and Description |
|---|---|
| public | CheckBoxTreeCell()
Creates a default |
| public | CheckBoxTreeCell(final Callback<TreeItem<T>, ObservableValue<Boolean>>
A getSelectedProperty)Callback that will return an
ObservableValue<Boolean> that represents whether the given
item is selected or not.Creates a |
| public | CheckBoxTreeCell(final Callback<TreeItem<T>, ObservableValue<Boolean>>
A getSelectedProperty, final StringConverter<TreeItem<T>> Callback that will return an
ObservableValue<Boolean> that represents whether the given
item is selected or not.A StringConverter that, give an object of type
TreeItem<T>, will return a String that can be used to represent the
object visually. converter)Creates a |
| private | CheckBoxTreeCell(final Callback<TreeItem<T>, ObservableValue<Boolean>> getSelectedProperty, final StringConverter<TreeItem<T>> converter, final Callback<TreeItem<T>, ObservableValue<Boolean>> getIndeterminateProperty)
|
| Modifier and Type | Method and Description |
|---|---|
| public final ObjectProperty | |
| public static < The type of the elements contained within the
T> CallbackCheckBoxTreeItem instances. | Returns: ACallback that will return a TreeCell that is able to
work on the type of element contained within the TreeView root, and
all of its children (recursively).Creates a cell factory for use in a TreeView control, although there is a
major assumption when used in a TreeView: this cell factory assumes that
the TreeView root, and all children are instances of
|
| public static < The type of the elements contained within the T> CallbackTreeItem
instances. | Returns: ACallback that will return a TreeCell that is able to
work on the type of element contained within the TreeView root, and
all of its children (recursively).A getSelectedProperty)Callback that, given an object of
type TreeItem<T>, will return an ObservableValue<Boolean>
that represents whether the given item is selected or not. This
ObservableValue<Boolean> will be bound bidirectionally
(meaning that the CheckBox in the cell will set/unset this property
based on user interactions, and the CheckBox will reflect the state
of the ObservableValue<Boolean>, if it changes externally).Creates a cell factory for use in a TreeView control. |
| public static < The type of the elements contained within the T> CallbackTreeItem
instances. | Returns: ACallback that will return a TreeCell that is able to
work on the type of element contained within the TreeView root, and
all of its children (recursively).A Callback that, given an object of
type TreeItem<T>, will return an getSelectedProperty, final StringConverter<TreeItem<T>> ObservableValue<Boolean>
that represents whether the given item is selected or not. This
ObservableValue<Boolean> will be bound bidirectionally
(meaning that the CheckBox in the cell will set/unset this property
based on user interactions, and the CheckBox will reflect the state of
the ObservableValue<Boolean>, if it changes externally).A StringConverter that, give an object of type
TreeItem<T>, will return a String that can be used to represent the
object visually. The default implementation in converter)forTreeView(Callback)
is to simply call .toString() on all non-null items (and to just
return an empty string in cases where the given item is null).Creates a cell factory for use in a TreeView control. |
| public final StringConverter | Returns: theStringConverter used in this cellReturns the |
| public final Callback | Returns: theCallback that is bound to by the CheckBox shown on screenReturns the |
| public Object | queryAccessibleAttribute(AccessibleAttribute attribute, Object... parameters)
Overrides javafx. This method is called by the assistive technology to request the value for an attribute. |
| public final ObjectProperty | Returns: the property representing theCallback that is bound to
by the CheckBox shown on screenProperty representing the |
| public final void | setConverter(StringConverter<TreeItem<T>>
the value)StringConverter to be used in this cellSets the |
| public final void | setSelectedStateCallback(Callback<TreeItem<T>, ObservableValue<Boolean>>
the value)Callback that is bound to by the CheckBox shown on screenSets the |
| pack-priv void | updateDisplay(T item, boolean empty)
Overrides javafx.
|
| public void | updateItem(T item, boolean empty)
Overrides javafx. The updateItem method should not be called by developers, but it is the best method for developers to override to allow for them to customise the visuals of the cell. |
| booleanProperty | back to summary |
|---|---|
| private ObservableValue<Boolean> booleanProperty | |
| checkBox | back to summary |
|---|---|
| private final CheckBox checkBox | |
| converter | back to summary |
|---|---|
| private ObjectProperty<StringConverter<TreeItem<T>>> converter | |
| indeterminateProperty | back to summary |
|---|---|
| private BooleanProperty indeterminateProperty | |
| selectedStateCallback | back to summary |
|---|---|
| private ObjectProperty<Callback<TreeItem<T>, ObservableValue<Boolean>>> selectedStateCallback | |
| CheckBoxTreeCell | back to summary |
|---|---|
| public CheckBoxTreeCell() Creates a default | |
| CheckBoxTreeCell | back to summary |
|---|---|
| public CheckBoxTreeCell(final Callback<TreeItem<T>, ObservableValue<Boolean>> getSelectedProperty) Creates a To call this method, it is necessary to provide a
If the items are not
| |
| CheckBoxTreeCell | back to summary |
|---|---|
| public CheckBoxTreeCell(final Callback<TreeItem<T>, ObservableValue<Boolean>> getSelectedProperty, final StringConverter<TreeItem<T>> converter) Creates a To call this method, it is necessary to provide a If the items are not
| |
| CheckBoxTreeCell | back to summary |
|---|---|
| private CheckBoxTreeCell(final Callback<TreeItem<T>, ObservableValue<Boolean>> getSelectedProperty, final StringConverter<TreeItem<T>> converter, final Callback<TreeItem<T>, ObservableValue<Boolean>> getIndeterminateProperty) | |
| converterProperty | back to summary |
|---|---|
| public final ObjectProperty The
| |
| forTreeView | back to summary |
|---|---|
| public static <T> Callback Creates a cell factory for use in a TreeView control, although there is a
major assumption when used in a TreeView: this cell factory assumes that
the TreeView root, and all children are instances of
When used in a TreeView, the CheckBoxCell is rendered with a CheckBox
to the right of the 'disclosure node' (i.e. the arrow). The item stored
in
Unfortunately, due to limitations in Java, it is necessary to provide an explicit cast when using this method. For example:
| |
| forTreeView | back to summary |
|---|---|
| public static <T> Callback Creates a cell factory for use in a TreeView control. Unlike
When used in a TreeView, the CheckBoxCell is rendered with a CheckBox
to the right of the 'disclosure node' (i.e. the arrow). The item stored
in Unlike
| |
| forTreeView | back to summary |
|---|---|
| public static <T> Callback Creates a cell factory for use in a TreeView control. Unlike
When used in a TreeView, the CheckBoxCell is rendered with a CheckBox
to the right of the 'disclosure node' (i.e. the arrow). The item stored
in Unlike
| |
| getConverter | back to summary |
|---|---|
| public final StringConverter Returns the
| |
| getSelectedStateCallback | back to summary |
|---|---|
| public final Callback Returns the
| |
| queryAccessibleAttribute | back to summary |
|---|---|
| public Object queryAccessibleAttribute(AccessibleAttribute attribute, Object... parameters) Overrides javafx. Doc from javafx. This method is called by the assistive technology to request the value for an attribute.
This method is commonly overridden by subclasses to implement
attributes that are required for a specific role.
| |
| selectedStateCallbackProperty | back to summary |
|---|---|
| public final ObjectProperty Property representing the
| |
| setConverter | back to summary |
|---|---|
| public final void setConverter(StringConverter<TreeItem<T>> value) Sets the
| |
| setSelectedStateCallback | back to summary |
|---|---|
| public final void setSelectedStateCallback(Callback<TreeItem<T>, ObservableValue<Boolean>> value) Sets the
| |
| updateDisplay | back to summary |
|---|---|
| pack-priv void updateDisplay(T item, boolean empty) Overrides javafx.
| |
| updateItem | back to summary |
|---|---|
| public void updateItem(T item, boolean empty) Overrides javafx. Doc from javafx. The updateItem method should not be called by developers, but it is the
best method for developers to override to allow for them to customise the
visuals of the cell. To clarify, developers should never call this method
in their code (they should leave it up to the UI control, such as the
It is very important that subclasses of Cell override the updateItem method properly, as failure to do so will lead to issues such as blank cells or cells with unexpected content appearing within them. Here is an example of how to properly override the updateItem method:
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setGraphic(null);
} else {
setText(item.toString());
}
}
Note in this code sample two important points:
| |