Class CheckBoxList

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

public class CheckBoxList extends JList
CheckBoxList is a special JList which uses JCheckBox as the list element. In addition to regular JList feature, it also allows you select any number of elements in the list by selecting the check boxes.

To select an element, user can mouse click on the check box, or highlight the rows and press SPACE key to toggle the selections.

We used cell renderer feature in JList to add the check box in each row. However you can still set your own cell renderer just like before using JList.setCellRenderer(javax.swing.ListCellRenderer). CheckBoxList will use your cell renderer and automatically put a check box before it.

The selection state is kept in a ListSelectionModel called CheckBoxListSelectionModel, which you can get using getCheckBoxListSelectionModel(). If you need to add a check to a check box or to find out if a check box is checked, you need to ask the getCheckBoxListSelectionModel() by using addListSelectionListener.

It is possible to add an "(All)" item. All you need to do is to add CheckBoxList.ALL_ENTRY to the list model. Then check the (All) item will select all the check boxes and uncheck it will deselect all.

Please note, we changed CheckBoxList implementation in 1.9.2 release. The old CheckBoxList class is renamed to CheckBoxListWithSelectable. If you want to use the old implementation, you can use CheckBoxListWithSelectable instead. The main difference between the two implementation is at how the selection state is kept. In new implementation, the selection state is kept at a separate ListSelectionModel which you can get using getCheckBoxListSelectionModel(). If you need to add a check to a check box or to find out if a check box is checked, you need to ask the getCheckBoxListSelectionModel() by using addListSelectionListener. The old implementation kept the selection state at Selectable object in the ListModel. The new implementation also has the same design as that of CheckBoxTree.

See Also:
  • Field Details

  • Constructor Details

    • CheckBoxList

      public CheckBoxList()
      Constructs a CheckBoxList with an empty model.
    • CheckBoxList

      public CheckBoxList(Vector<?> listData)
      Constructs a CheckBoxList that displays the elements in the specified Vector.
      Parameters:
      listData - the Vector to be loaded into the data model
    • CheckBoxList

      public CheckBoxList(Object[] listData)
      Constructs a CheckBoxList that displays the elements in the specified Object[].
      Parameters:
      listData - the array of Objects to be loaded into the data model
    • CheckBoxList

      public CheckBoxList(ListModel dataModel)
      Constructs a CheckBoxList that displays the elements in the specified, non-null model. All CheckBoxList constructors delegate to this one.

      Parameters:
      dataModel - the data model for this list
      Throws:
      IllegalArgumentException - if dataModel is null
  • Method Details

    • setModel

      public void setModel(ListModel model)
      Overrides:
      setModel in class JList
    • updateUI

      public void updateUI()
      Overrides:
      updateUI in class JList
    • init

      protected void init()
      Initialize the CheckBoxList.
    • getLastVisibleIndex

      public int getLastVisibleIndex()
      Overrides:
      getLastVisibleIndex in class JList
    • createCheckBoxListSelectionModel

      protected CheckBoxListSelectionModel createCheckBoxListSelectionModel(ListModel model)
    • createCellRenderer

      protected CheckBoxListCellRenderer createCellRenderer()
      Creates the cell renderer.
      Returns:
      the cell renderer.
    • createHandler

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

      public ListCellRenderer getCellRenderer()
      Overrides:
      getCellRenderer in class JList
    • getActualCellRenderer

      public ListCellRenderer getActualCellRenderer()
    • getNextMatch

      public int getNextMatch(String prefix, int startIndex, Position.Bias bias)
      Overrides:
      getNextMatch in class JList
    • isCheckBoxEnabled

      public boolean isCheckBoxEnabled(int index)
      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.
      Parameters:
      index - the row index.
      Returns:
      true or false. If false, the check box on the particular row index will be disabled.
    • isCheckBoxVisible

      public boolean isCheckBoxVisible(int index)
      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:
      index - whether the check box on the row index is visible.
      Returns:
      true or false. If false, there is not check box on the particular row index. By default, we always return true. You override this method to return true of false depending on your need.
    • 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.
    • isClickInCheckBoxOnly

      public boolean isClickInCheckBoxOnly()
      Gets the value of property clickInCheckBoxOnly. When clickInCheckBoxOnly is false, clicking on anywhere of an row will do select the row and toggle check/uncheck. When clickInCheckBoxOnly is true, only click on checkbox will change check/uncheck status without affecting selected rows, and click on label will only select the row 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.
    • getCheckBoxListSelectionModel

      public CheckBoxListSelectionModel getCheckBoxListSelectionModel()
      Gets the ListSelectionModel that keeps the check boxes' state information for CheckBoxList.
      Returns:
      the ListSelectionModel that keeps the check boxes' state information for CheckBoxList.
    • setCheckBoxListSelectionModel

      public void setCheckBoxListSelectionModel(CheckBoxListSelectionModel checkBoxListSelectionModel)
    • getCheckBoxListSelectedIndices

      public int[] getCheckBoxListSelectedIndices()
      Returns an array of all of the selected indices in increasing order.
      Returns:
      all of the selected indices, in increasing order
      See Also:
    • setCheckBoxListSelectedIndex

      public void setCheckBoxListSelectedIndex(int index)
      Selects a single cell and clear all other selections.
      Parameters:
      index - the index of the one cell to select
      See Also:
    • addCheckBoxListSelectedIndex

      public void addCheckBoxListSelectedIndex(int index)
      Selects a single cell and keeps all previous selections.
      Parameters:
      index - the index of the one cell to select
      See Also:
    • removeCheckBoxListSelectedIndex

      public void removeCheckBoxListSelectedIndex(int index)
      Deselects a single cell.
      Parameters:
      index - the index of the one cell to select
      See Also:
    • setCheckBoxListSelectedIndices

      public void setCheckBoxListSelectedIndices(int[] indices)
      Selects a set of cells.
      Parameters:
      indices - an array of the indices of the cells to select
      See Also:
    • setSelectedObjects

      public void setSelectedObjects(Object[] elements)
      Sets the selected elements.
      Parameters:
      elements - sets the select elements. All the rows that have the value in the array will be checked.
    • setSelectedObjects

      public void setSelectedObjects(Vector<?> elements)
      Sets the selected elements.
      Parameters:
      elements - sets the select elements. All the rows that have the value in the Vector will be checked.
    • getCheckBoxListSelectedValues

      public Object[] getCheckBoxListSelectedValues()
      Returns an array of the values for the selected cells. The returned values are sorted in increasing index order.
      Returns:
      the selected values or an empty list if nothing is selected
      See Also:
    • getCheckBoxListSelectedIndex

      public int getCheckBoxListSelectedIndex()
      Returns the first selected index; returns -1 if there is no selected item.
      Returns:
      the value of getMinSelectionIndex
      See Also:
    • getCheckBoxListSelectedValue

      public Object getCheckBoxListSelectedValue()
      Returns the first selected value, or null if the selection is empty.
      Returns:
      the first selected value
      See Also:
    • setCheckBoxListSelectedValue

      public void setCheckBoxListSelectedValue(Object anObject, boolean shouldScroll)
      Selects the specified object from the list and clear all other selections.
      Parameters:
      anObject - the object to select
      shouldScroll - true if the list should scroll to display the selected object, if one exists; otherwise false
    • addCheckBoxListSelectedValue

      public void addCheckBoxListSelectedValue(Object anObject, boolean shouldScroll)
      Selects the specified object from the list and keep all previous selections.
      Parameters:
      anObject - the object to be selected
      shouldScroll - true if the list should scroll to display the selected object, if one exists; otherwise false
    • addCheckBoxListSelectedValues

      public void addCheckBoxListSelectedValues(Object[] objects)
      Selects the specified objects from the list and keep all previous selections.
      Parameters:
      objects - the objects to be selected
    • removeCheckBoxListSelectedValues

      public void removeCheckBoxListSelectedValues(Object[] objects)
      Deselects the specified objects from the list and keep all previous selections.
      Parameters:
      objects - the objects to be selected
    • removeCheckBoxListSelectedValue

      public void removeCheckBoxListSelectedValue(Object anObject, boolean shouldScroll)
      Deselects the specified object from the list.
      Parameters:
      anObject - the object to select
      shouldScroll - true if the list should scroll to display the selected object, if one exists; otherwise false
    • clearCheckBoxListSelection

      public void clearCheckBoxListSelection()
    • selectAll

      public void selectAll()
      Selects all objects in this list.
    • selectNone

      public void selectNone()
      Deselects all objects in this list.
    • getPreferredScrollableViewportSize

      public Dimension getPreferredScrollableViewportSize()
      Specified by:
      getPreferredScrollableViewportSize in interface Scrollable
      Overrides:
      getPreferredScrollableViewportSize in class JList