Class CheckBoxTreeCell<T>

Type Parameters:
T - The type of the elements contained within the TreeView TreeItem instances.
All Implemented Interfaces:
Styleable, EventTarget, Skinnable

public class CheckBoxTreeCell<T> extends TreeCell<T>
A class containing a 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 TreeItem.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:

  • Clicking on the CheckBox beside an item that has children will result in all children also becoming selected/unselected.
  • Clicking on the 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.
If it is decided that using 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.

Since:
JavaFX 2.2
  • Property Details

  • Constructor Details

    • CheckBoxTreeCell

      public CheckBoxTreeCell()
      Creates a default CheckBoxTreeCell that assumes the TreeView is constructed with CheckBoxTreeItem instances, rather than the default TreeItem. By using CheckBoxTreeItem, it will internally manage the selected and indeterminate state of each item in the tree.
    • CheckBoxTreeCell

      public CheckBoxTreeCell(Callback<TreeItem<T>,ObservableValue<Boolean>> getSelectedProperty)
      Creates a CheckBoxTreeCell for use in a TreeView control via a cell factory. Unlike CheckBoxTreeCell(), this method does not assume that all TreeItem instances in the TreeView are CheckBoxTreeItem.

      To call this method, it is necessary to provide a 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).

      If the items are not CheckBoxTreeItem instances, it becomes the developers responsibility to handle updating the state of parent and children TreeItems. This means that, given a TreeItem, this class will simply toggles the ObservableValue<Boolean> that is provided, and no more. Of course, this functionality can then be implemented externally by adding observers to the ObservableValue<Boolean>, and toggling the state of other properties as necessary.

      Parameters:
      getSelectedProperty - A Callback that will return an ObservableValue<Boolean> that represents whether the given item is selected or not.
    • CheckBoxTreeCell

      public CheckBoxTreeCell(Callback<TreeItem<T>,ObservableValue<Boolean>> getSelectedProperty, StringConverter<TreeItem<T>> converter)
      Creates a CheckBoxTreeCell for use in a TreeView control via a cell factory. Unlike CheckBoxTreeCell(), this method does not assume that all TreeItem instances in the TreeView are CheckBoxTreeItem.

      To call this method, it is necessary to provide a 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).

      If the items are not CheckBoxTreeItem instances, it becomes the developers responsibility to handle updating the state of parent and children TreeItems. This means that, given a TreeItem, this class will simply toggles the ObservableValue<Boolean> that is provided, and no more. Of course, this functionality can then be implemented externally by adding observers to the ObservableValue<Boolean>, and toggling the state of other properties as necessary.

      Parameters:
      getSelectedProperty - A Callback that will return an ObservableValue<Boolean> that represents whether the given item is selected or not.
      converter - A StringConverter that, give an object of type TreeItem<T>, will return a String that can be used to represent the object visually.
  • Method Details

    • forTreeView

      public static <T> Callback<TreeView<T>,TreeCell<T>> forTreeView()
      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 CheckBoxTreeItem, rather than the default TreeItem class that is used normally.

      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 TreeItem.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:

      • Clicking on the CheckBox beside an item that has children will result in all children also becoming selected/unselected.
      • Clicking on the 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.

      Unfortunately, due to limitations in Java, it is necessary to provide an explicit cast when using this method. For example:

       
       final TreeView<String> treeView = new TreeView<String>();
       treeView.setCellFactory(CheckBoxCell.<String>forTreeView());
      Type Parameters:
      T - The type of the elements contained within the CheckBoxTreeItem instances.
      Returns:
      A Callback 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).
    • forTreeView

      public static <T> Callback<TreeView<T>,TreeCell<T>> forTreeView(Callback<TreeItem<T>,ObservableValue<Boolean>> getSelectedProperty)
      Creates a cell factory for use in a TreeView control. Unlike forTreeView(), this method does not assume that all TreeItem instances in the TreeView are CheckBoxTreeItem instances.

      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 TreeItem.getValue() will then have the StringConverter called on it, and this text will take all remaining horizontal space.

      Unlike forTreeView(), this cell factory does not handle updating the state of parent or children TreeItems - it simply toggles the ObservableValue<Boolean> that is provided, and no more. Of course, this functionality can then be implemented externally by adding observers to the ObservableValue<Boolean>, and toggling the state of other properties as necessary.

      Type Parameters:
      T - The type of the elements contained within the TreeItem instances.
      Parameters:
      getSelectedProperty - A 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).
      Returns:
      A Callback 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).
    • forTreeView

      public static <T> Callback<TreeView<T>,TreeCell<T>> forTreeView(Callback<TreeItem<T>,ObservableValue<Boolean>> getSelectedProperty, StringConverter<TreeItem<T>> converter)
      Creates a cell factory for use in a TreeView control. Unlike forTreeView(), this method does not assume that all TreeItem instances in the TreeView are CheckBoxTreeItem.

      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 TreeItem.getValue() will then have the the StringConverter called on it, and this text will take all remaining horizontal space.

      Unlike forTreeView(), this cell factory does not handle updating the state of parent or children TreeItems - it simply toggles the ObservableValue<Boolean> that is provided, and no more. Of course, this functionality can then be implemented externally by adding observers to the ObservableValue<Boolean>, and toggling the state of other properties as necessary.

      Type Parameters:
      T - The type of the elements contained within the TreeItem instances.
      Parameters:
      getSelectedProperty - A 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).
      converter - 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 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).
      Returns:
      A Callback 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).
    • converterProperty

      public final ObjectProperty<StringConverter<TreeItem<T>>> converterProperty()
      The StringConverter property.
      See Also:
    • setConverter

      public final void setConverter(StringConverter<TreeItem<T>> value)
      Sets the StringConverter to be used in this cell.
      Parameters:
      value - the StringConverter to be used in this cell
    • getConverter

      public final StringConverter<TreeItem<T>> getConverter()
      Returns the StringConverter used in this cell.
      Returns:
      the StringConverter used in this cell
    • selectedStateCallbackProperty

      public final ObjectProperty<Callback<TreeItem<T>,ObservableValue<Boolean>>> selectedStateCallbackProperty()
      Property representing the Callback that is bound to by the CheckBox shown on screen.
      See Also:
    • setSelectedStateCallback

      public final void setSelectedStateCallback(Callback<TreeItem<T>,ObservableValue<Boolean>> value)
      Sets the Callback that is bound to by the CheckBox shown on screen.
      Parameters:
      value - the Callback that is bound to by the CheckBox shown on screen
    • getSelectedStateCallback

      public final Callback<TreeItem<T>,ObservableValue<Boolean>> getSelectedStateCallback()
      Returns the Callback that is bound to by the CheckBox shown on screen.
      Returns:
      the Callback that is bound to by the CheckBox shown on screen
    • updateItem

      public void updateItem(T item, boolean empty)
      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 ListView control) to call this method. However, the purpose of having the updateItem method is so that developers, when specifying custom cell factories (again, like the ListView cell factory), the updateItem method can be overridden to allow for complete customisation of the cell.

      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:

      1. We call the super.updateItem(T, boolean) method. If this is not done, the item and empty properties are not correctly set, and you are likely to end up with graphical issues.
      2. We test for the empty condition, and if true, we set the text and graphic properties to null. If we do not do this, it is almost guaranteed that end users will see graphical artifacts in cells unexpectedly.
      Parameters:
      item - The new item for the cell.
      empty - whether or not this cell represents data from the list. If it is empty, then it does not represent any domain data, but is a cell being used to render an "empty" row.