public class CheckBoxList
extends javax.swing.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.
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
.Modifier and Type | Class and Description |
---|---|
protected static class |
CheckBoxList.Handler |
javax.swing.JList.AccessibleJList, javax.swing.JList.DropLocation
javax.swing.JComponent.AccessibleJComponent
Modifier and Type | Field and Description |
---|---|
protected CheckBoxList.Handler |
_handler |
protected CheckBoxListCellRenderer |
_listCellRenderer |
static java.lang.String |
PROPERTY_CHECKBOX_ENABLED |
static java.lang.String |
PROPERTY_CLICK_IN_CHECKBOX_ONLY |
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Constructor and Description |
---|
CheckBoxList()
Constructs a
CheckBoxList with an empty model. |
CheckBoxList(javax.swing.ListModel dataModel)
Constructs a
CheckBoxList that displays the elements in the specified, non-null model. |
CheckBoxList(java.lang.Object[] listData)
Constructs a
CheckBoxList that displays the elements in the specified Object[] . |
CheckBoxList(java.util.Vector<?> listData)
Constructs a
CheckBoxList that displays the elements in the specified Vector . |
Modifier and Type | Method and Description |
---|---|
void |
addCheckBoxListSelectedIndex(int index)
Selects a single cell and keeps all previous selections.
|
void |
addCheckBoxListSelectedValue(java.lang.Object anObject,
boolean shouldScroll)
Selects the specified object from the list and keep all previous selections.
|
void |
addCheckBoxListSelectedValues(java.lang.Object[] objects)
Selects the specified objects from the list and keep all previous selections.
|
void |
clearCheckBoxListSelection() |
protected CheckBoxListCellRenderer |
createCellRenderer()
Creates the cell renderer.
|
protected CheckBoxListSelectionModel |
createCheckBoxListSelectionModel(javax.swing.ListModel model) |
protected CheckBoxList.Handler |
createHandler()
Creates the mouse listener and key listener used by CheckBoxList.
|
javax.swing.ListCellRenderer |
getActualCellRenderer() |
javax.swing.ListCellRenderer |
getCellRenderer() |
int |
getCheckBoxListSelectedIndex()
Returns the first selected index; returns -1 if there is no selected item.
|
int[] |
getCheckBoxListSelectedIndices()
Returns an array of all of the selected indices in increasing order.
|
java.lang.Object |
getCheckBoxListSelectedValue()
Returns the first selected value, or
null if the selection is empty. |
java.lang.Object[] |
getCheckBoxListSelectedValues()
Returns an array of the values for the selected cells.
|
CheckBoxListSelectionModel |
getCheckBoxListSelectionModel()
Gets the ListSelectionModel that keeps the check boxes' state information for CheckBoxList.
|
int |
getLastVisibleIndex() |
int |
getNextMatch(java.lang.String prefix,
int startIndex,
javax.swing.text.Position.Bias bias) |
java.awt.Dimension |
getPreferredScrollableViewportSize() |
java.lang.String |
getUIClassID()
Returns a string that specifies the name of the L&F class that renders this component.
|
protected void |
init()
Initialize the CheckBoxList.
|
boolean |
isCheckBoxEnabled()
Gets the value of property checkBoxEnabled.
|
boolean |
isCheckBoxEnabled(int index)
Checks if check box is enabled.
|
boolean |
isCheckBoxVisible(int index)
Checks if check box is visible.
|
boolean |
isClickInCheckBoxOnly()
Gets the value of property clickInCheckBoxOnly.
|
void |
removeCheckBoxListSelectedIndex(int index)
Deselects a single cell.
|
void |
removeCheckBoxListSelectedValue(java.lang.Object anObject,
boolean shouldScroll)
Deselects the specified object from the list.
|
void |
selectAll()
Selects all objects in this list except those are disabled.
|
void |
selectNone()
Deselects all objects in this list except those are disabled.
|
void |
setCheckBoxEnabled(boolean checkBoxEnabled)
Sets the value of property checkBoxEnabled.
|
void |
setCheckBoxListSelectedIndex(int index)
Selects a single cell and clear all other selections.
|
void |
setCheckBoxListSelectedIndices(int[] indices)
Selects a set of cells.
|
void |
setCheckBoxListSelectedValue(java.lang.Object anObject,
boolean shouldScroll)
Selects the specified object from the list and clear all other selections.
|
void |
setCheckBoxListSelectionModel(CheckBoxListSelectionModel checkBoxListSelectionModel) |
void |
setClickInCheckBoxOnly(boolean clickInCheckBoxOnly)
Sets the value of property clickInCheckBoxOnly.
|
void |
setSelectedObjects(java.lang.Object[] elements)
Sets the selected elements.
|
void |
setSelectedObjects(java.util.Vector<?> elements)
Sets the selected elements.
|
void |
updateUI()
Resets the UI property to a value from the current look and feel.
|
addListSelectionListener, addSelectionInterval, clearSelection, createSelectionModel, ensureIndexIsVisible, fireSelectionValueChanged, getAccessibleContext, getAnchorSelectionIndex, getCellBounds, getDragEnabled, getDropLocation, getDropMode, getFirstVisibleIndex, getFixedCellHeight, getFixedCellWidth, getLayoutOrientation, getLeadSelectionIndex, getListSelectionListeners, getMaxSelectionIndex, getMinSelectionIndex, getModel, getPrototypeCellValue, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedIndex, getSelectedIndices, getSelectedValue, getSelectedValues, getSelectedValuesList, getSelectionBackground, getSelectionForeground, getSelectionMode, getSelectionModel, getToolTipText, getUI, getValueIsAdjusting, getVisibleRowCount, indexToLocation, isSelectedIndex, isSelectionEmpty, locationToIndex, paramString, removeListSelectionListener, removeSelectionInterval, setCellRenderer, setDragEnabled, setDropMode, setFixedCellHeight, setFixedCellWidth, setLayoutOrientation, setListData, setListData, setModel, setPrototypeCellValue, setSelectedIndex, setSelectedIndices, setSelectedValue, setSelectionBackground, setSelectionForeground, setSelectionInterval, setSelectionMode, setSelectionModel, setUI, setValueIsAdjusting, setVisibleRowCount
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
protected CheckBoxListCellRenderer _listCellRenderer
public static final java.lang.String PROPERTY_CHECKBOX_ENABLED
public static final java.lang.String PROPERTY_CLICK_IN_CHECKBOX_ONLY
protected CheckBoxList.Handler _handler
public CheckBoxList()
CheckBoxList
with an empty model.public CheckBoxList(java.util.Vector<?> listData)
CheckBoxList
that displays the elements in the specified Vector
.listData
- the Vector
to be loaded into the data modelpublic CheckBoxList(java.lang.Object[] listData)
CheckBoxList
that displays the elements in the specified Object[]
.listData
- the array of Objects to be loaded into the data modelpublic CheckBoxList(javax.swing.ListModel dataModel)
CheckBoxList
that displays the elements in the specified, non-null
model.
All CheckBoxList
constructors delegate to this one.
dataModel
- the data model for this listjava.lang.IllegalArgumentException
- if dataModel
is null
public void updateUI()
updateUI
in class javax.swing.JList
JComponent.updateUI()
public java.lang.String getUIClassID()
getUIClassID
in class javax.swing.JList
JComponent.getUIClassID()
,
UIDefaults.getUI(javax.swing.JComponent)
protected void init()
public int getLastVisibleIndex()
getLastVisibleIndex
in class javax.swing.JList
protected CheckBoxListSelectionModel createCheckBoxListSelectionModel(javax.swing.ListModel model)
protected CheckBoxListCellRenderer createCellRenderer()
protected CheckBoxList.Handler createHandler()
public javax.swing.ListCellRenderer getCellRenderer()
getCellRenderer
in class javax.swing.JList
public javax.swing.ListCellRenderer getActualCellRenderer()
public int getNextMatch(java.lang.String prefix, int startIndex, javax.swing.text.Position.Bias bias)
getNextMatch
in class javax.swing.JList
public boolean isCheckBoxEnabled(int index)
index
- the row index.public boolean isCheckBoxVisible(int index)
index
- whether the check box on the row index is visible.public boolean isCheckBoxEnabled()
public void setCheckBoxEnabled(boolean checkBoxEnabled)
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.public boolean isClickInCheckBoxOnly()
public void setClickInCheckBoxOnly(boolean clickInCheckBoxOnly)
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.public CheckBoxListSelectionModel getCheckBoxListSelectionModel()
public void setCheckBoxListSelectionModel(CheckBoxListSelectionModel checkBoxListSelectionModel)
public int[] getCheckBoxListSelectedIndices()
JList.removeSelectionInterval(int, int)
,
JList.addListSelectionListener(javax.swing.event.ListSelectionListener)
public void setCheckBoxListSelectedIndex(int index)
index
- the index of the one cell to selectListSelectionModel.setSelectionInterval(int, int)
,
JList.isSelectedIndex(int)
,
JList.addListSelectionListener(javax.swing.event.ListSelectionListener)
public void addCheckBoxListSelectedIndex(int index)
index
- the index of the one cell to selectListSelectionModel.setSelectionInterval(int, int)
,
JList.isSelectedIndex(int)
,
JList.addListSelectionListener(javax.swing.event.ListSelectionListener)
public void removeCheckBoxListSelectedIndex(int index)
index
- the index of the one cell to selectListSelectionModel.setSelectionInterval(int, int)
,
JList.isSelectedIndex(int)
,
JList.addListSelectionListener(javax.swing.event.ListSelectionListener)
public void setCheckBoxListSelectedIndices(int[] indices)
indices
- an array of the indices of the cells to selectListSelectionModel.addSelectionInterval(int, int)
,
JList.isSelectedIndex(int)
,
JList.addListSelectionListener(javax.swing.event.ListSelectionListener)
public void setSelectedObjects(java.lang.Object[] elements)
elements
- sets the select elements. All the rows that have the value in the array will be checked.public void setSelectedObjects(java.util.Vector<?> elements)
elements
- sets the select elements. All the rows that have the value in the Vector will be checked.public java.lang.Object[] getCheckBoxListSelectedValues()
JList.isSelectedIndex(int)
,
JList.getModel()
,
JList.addListSelectionListener(javax.swing.event.ListSelectionListener)
public int getCheckBoxListSelectedIndex()
getMinSelectionIndex
JList.getMinSelectionIndex()
,
JList.addListSelectionListener(javax.swing.event.ListSelectionListener)
public java.lang.Object getCheckBoxListSelectedValue()
null
if the selection is empty.JList.getMinSelectionIndex()
,
JList.getModel()
,
JList.addListSelectionListener(javax.swing.event.ListSelectionListener)
public void setCheckBoxListSelectedValue(java.lang.Object anObject, boolean shouldScroll)
anObject
- the object to selectshouldScroll
- true if the list should scroll to display the selected object, if one exists; otherwise
falsepublic void addCheckBoxListSelectedValue(java.lang.Object anObject, boolean shouldScroll)
anObject
- the object to be selectedshouldScroll
- true if the list should scroll to display the selected object, if one exists; otherwise
falsepublic void addCheckBoxListSelectedValues(java.lang.Object[] objects)
objects
- the objects to be selectedpublic void removeCheckBoxListSelectedValue(java.lang.Object anObject, boolean shouldScroll)
anObject
- the object to selectshouldScroll
- true if the list should scroll to display the selected object, if one exists; otherwise
falsepublic void clearCheckBoxListSelection()
public void selectAll()
public void selectNone()
public java.awt.Dimension getPreferredScrollableViewportSize()
getPreferredScrollableViewportSize
in interface javax.swing.Scrollable
getPreferredScrollableViewportSize
in class javax.swing.JList