Class CheckBoxListWithSelectable

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

public class CheckBoxListWithSelectable extends JList implements ItemSelectable
CheckBoxListWithSelectable 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.

The element is ListModel should be an instance of Selectable. If you have your own class that represents the element in the list, you can implement Selectable and implements a few very simple methods. If your elements are already in an array or Vector that you pass in to the constructor of JList, we will convert them to DefaultSelectable which implements Selectable interface.

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

To listen to the check box selection change, you can call addItemListener to add an ItemListener.

Please note, there are two implementations of CheckBoxList. CheckBoxListWithSelectable is one. There is also another one call CheckBoxList. CheckBoxListWithSelectable is actually the old implementation. In 1.9.2, we introduced a new implementation and renamed the old implementation to CheckBoxListWithSelectable. 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 CheckBoxList.getCheckBoxListSelectionModel(). The old implementation kept the selection state at Selectable object in the ListModel.

See Also:
  • Field Details

  • Constructor Details

    • CheckBoxListWithSelectable

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

      public CheckBoxListWithSelectable(Vector<?> listData)
      Constructs a CheckBoxList that displays the elements in the specified Vector. If the Vector contains elements which is not an instance of Selectable, it will wrap it automatically into DefaultSelectable and add to ListModel.
      Parameters:
      listData - the Vector to be loaded into the data model
    • CheckBoxListWithSelectable

      public CheckBoxListWithSelectable(Object[] listData)
      Constructs a CheckBoxList that displays the elements in the specified Object[]. If the Object array contains elements which is not an instance of Selectable, it will wrap it automatically into DefaultSelectable and add to ListModel.
      Parameters:
      listData - the array of Objects to be loaded into the data model
    • CheckBoxListWithSelectable

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

      Please note, if you are using this constructor, please make sure all elements in dataModel are instance of Selectable.

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

    • init

      protected void init()
      Initialize the CheckBoxList.
    • createCellRenderer

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

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

      public void setSelectedObjects(Object[] elements)
      Sets the selected elements.
      Parameters:
      elements - the elements to be selected
    • setSelectedObjects

      public void setSelectedObjects(Vector<?> objects)
      Sets the selected objects.
      Parameters:
      objects - the elements to be selected in a Vector.
    • getCellRenderer

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

      public ListCellRenderer getActualCellRenderer()
    • isClickInCheckBoxOnly

      public boolean isClickInCheckBoxOnly()
      Gets the value of property clickInCheckBoxOnly. 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 clickInCheckBoxOnly.
    • setClickInCheckBoxOnly

      public void setClickInCheckBoxOnly(boolean clickInCheckBoxOnly)
      Sets the value of property clickInCheckBoxOnly.
      Parameters:
      clickInCheckBoxOnly - 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.
    • addItemListener

      public void addItemListener(ItemListener listener)
      Adds a listener to the list that's notified each time a change to the item selection occurs. Listeners added directly to the CheckBoxList will have their ItemEvent.getSource() == this CheckBoxList.
      Specified by:
      addItemListener in interface ItemSelectable
      Parameters:
      listener - the ItemListener to add
    • removeItemListener

      public void removeItemListener(ItemListener listener)
      Removes a listener from the list that's notified each time a change to the item selection occurs.
      Specified by:
      removeItemListener in interface ItemSelectable
      Parameters:
      listener - the ItemListener to remove
    • getItemListeners

      public ItemListener[] getItemListeners()
      Returns an array of all the ItemListeners added to this JList with addItemListener().
      Returns:
      all of the ItemListeners added or an empty array if no listeners have been added
      See Also:
    • fireItemStateChanged

      protected void fireItemStateChanged(ItemEvent event)
      Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the event parameter.
      Parameters:
      event - the ItemEvent object
      See Also:
    • getSelectedObjects

      public Object[] getSelectedObjects()
      Gets the selected objects. This is different from JList.getSelectedValues() which is a JList's feature. The List returned from this method contains the objects that is checked in the CheckBoxList.
      Specified by:
      getSelectedObjects in interface ItemSelectable
      Returns:
      the selected objects.
    • selectAll

      public void selectAll()
      Selects all objects in this list except those are disabled.
    • selectNone

      public void selectNone()
      Deselects all objects in this list except those are disabled.
    • setListData

      public void setListData(Vector listData)
      Overrides:
      setListData in class JList
    • setListData

      public void setListData(Object[] listData)
      Overrides:
      setListData in class JList
    • getNextMatch

      public int getNextMatch(String prefix, int startIndex, Position.Bias bias)
      Overrides:
      getNextMatch in class JList
    • 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.
    • 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 - the row index.
      Returns:
      true or false. If false, there is not check box on the particular row index.
    • setCheckBoxEnabled

      public void setCheckBoxEnabled(boolean checkBoxEnabled)
      Sets the value of property checkBoxEnabled.
      Parameters:
      checkBoxEnabled - true to enable all the check boxes. False to disable all of them.