Class CheckBoxTree

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, Scrollable

public class CheckBoxTree extends JTree
CheckBoxTree is a special JTree which uses JCheckBox as the tree renderer. In addition to regular JTree's features, it also allows you select any number of tree nodes in the tree by selecting the check boxes.

To select an element, user can mouse click on the check box, or select one or several tree nodes and press SPACE key to toggle the check box selection for all selected tree nodes.

In order to retrieve which tree paths are selected, you need to call getCheckBoxTreeSelectionModel(). It will return the selection model that keeps track of which tree paths have been checked. For example DefaultTreeSelectionModel.getSelectionPaths() will give the list of paths which have been checked.

See Also:
  • Field Details

  • Constructor Details

    • CheckBoxTree

      public CheckBoxTree()
    • CheckBoxTree

      public CheckBoxTree(Object[] value)
    • CheckBoxTree

      public CheckBoxTree(Vector<?> value)
    • CheckBoxTree

      public CheckBoxTree(Hashtable<?,?> value)
    • CheckBoxTree

      public CheckBoxTree(TreeNode root)
    • CheckBoxTree

      public CheckBoxTree(TreeNode root, boolean asksAllowsChildren)
    • CheckBoxTree

      public CheckBoxTree(TreeModel newModel)
  • Method Details

    • init

      protected void init()
      Initialize the CheckBoxTree.
    • createCheckBoxTreeSelectionModel

      protected CheckBoxTreeSelectionModel createCheckBoxTreeSelectionModel(TreeModel model)
      Creates the CheckBoxTreeSelectionModel.
      Parameters:
      model - the tree model.
      Returns:
      the CheckBoxTreeSelectionModel.
    • getCellRenderer

      public TreeCellRenderer getCellRenderer()
      Gets the cell renderer with check box.
      Overrides:
      getCellRenderer in class JTree
      Returns:
      CheckBoxTree's own cell renderer which has the check box. The actual cell renderer you set by setCellRenderer() can be accessed by using getActualCellRenderer().
    • getActualCellRenderer

      public TreeCellRenderer getActualCellRenderer()
      Gets the actual cell renderer. Since CheckBoxTree has its own check box cell renderer, this method will give you access to the actual cell renderer which is either the default tree cell renderer or the cell renderer you set using setCellRenderer(javax.swing.tree.TreeCellRenderer).
      Returns:
      the actual cell renderer
    • setCellRenderer

      public void setCellRenderer(TreeCellRenderer x)
      Overrides:
      setCellRenderer in class JTree
    • createCellRenderer

      protected CheckBoxTreeCellRenderer createCellRenderer(TreeCellRenderer renderer)
      Creates the cell renderer.
      Parameters:
      renderer - the actual renderer for the tree node. This method will return a cell renderer that use a check box and put the actual renderer inside it.
      Returns:
      the cell renderer.
    • createHandler

      protected CheckBoxTree.Handler createHandler()
      Creates the mouse listener and key listener used by CheckBoxTree.
      Returns:
      the Handler.
    • getCheckBox

      public TristateCheckBox getCheckBox()
      Get the CheckBox used for CheckBoxTreeCellRenderer.
      Returns:
      the check box.
      See Also:
    • setCheckBox

      public void setCheckBox(TristateCheckBox checkBox)
      Set the CheckBox used for CheckBoxTreeCellRenderer.

      By default, it's null. CheckBoxTreeCellRenderer then will create a default TristateCheckBox.

      Parameters:
      checkBox - the check box
    • isSelectPartialOnToggling

      public boolean isSelectPartialOnToggling()
      Gets the flag indicating if toggling should select or deselect the partially selected node.
      Returns:
      true if select first. Otherwise false.
      See Also:
    • setSelectPartialOnToggling

      public void setSelectPartialOnToggling(boolean selectPartialOnToggling)
      Sets the flag indicating if toggling should select or deselect the partially selected node.

      By default, the value is true to keep original behavior.

      Parameters:
      selectPartialOnToggling - the flag
    • getNextMatch

      public TreePath getNextMatch(String prefix, int startingRow, Position.Bias bias)
      Overrides:
      getNextMatch in class JTree
    • getCheckBoxTreeSelectionModel

      public CheckBoxTreeSelectionModel getCheckBoxTreeSelectionModel()
      Gets the selection model for the check boxes. To retrieve the state of check boxes, you should use this selection model.
      Returns:
      the selection model for the check boxes.
    • isCheckBoxEnabled

      public boolean isCheckBoxEnabled()
      Gets the value of property checkBoxEnabled. If true, user can click on check boxes on each tree node to select and deselect. If false, user can't click but you as developer can programmatically call API to select/deselect it.
      Returns:
      the value of property checkBoxEnabled.
    • setCheckBoxEnabled

      public void setCheckBoxEnabled(boolean checkBoxEnabled)
      Sets the value of property checkBoxEnabled.
      Parameters:
      checkBoxEnabled - true to allow to check the check box. False to disable it which means user can see whether a row is checked or not but they cannot change it.
    • isCheckBoxEnabled

      public boolean isCheckBoxEnabled(TreePath path)
      Checks if check box is enabled. There is no setter for it. The only way is to override this method to return true or false.

      However, in digIn mode, user can still select the disabled node by selecting all children nodes of that node. Also if user selects the parent node, the disabled children nodes will be selected too.

      Parameters:
      path - the tree path.
      Returns:
      true or false. If false, the check box on the particular tree path will be disabled.
    • isCheckBoxVisible

      public boolean isCheckBoxVisible(TreePath path)
      Checks if check box is visible. There is no setter for it. The only way is to override this method to return true or false.
      Parameters:
      path - the tree path.
      Returns:
      true or false. If false, the check box on the particular tree path will be disabled.
    • isDigIn

      public boolean isDigIn()
      Gets the dig-in mode. If the CheckBoxTree is in dig-in mode, checking the parent node will check all the children. Correspondingly, getSelectionPaths() will only return the parent tree path. If not in dig-in mode, each tree node can be checked or unchecked independently
      Returns:
      true or false.
    • setDigIn

      public void setDigIn(boolean digIn)
      Sets the dig-in mode. If the CheckBoxTree is in dig-in mode, checking the parent node will check all the children. Correspondingly, getSelectionPaths() will only return the parent tree path. If not in dig-in mode, each tree node can be checked or unchecked independently
      Parameters:
      digIn - the new digIn mode.
    • isClickInCheckBoxOnly

      public boolean isClickInCheckBoxOnly()
      Gets the value of property clickInCheckBoxOnly. When clickInCheckBoxOnly is false, clicking on anywhere of an tree node will do select the tree node and toggle check/uncheck. When clickInCheckBoxOnly is true, only click on checkbox will change check/uncheck status without affecting selected tree nodes, and click on label will only select the tree node without affecting check/uncheck status.
      Returns:
      the value of property clickInCheckBoxOnly.
    • setClickInCheckBoxOnly

      public void setClickInCheckBoxOnly(boolean clickInCheckBoxOnly)
      Sets the value of property clickInCheckBoxOnly.
      Parameters:
      clickInCheckBoxOnly - see for more information.