public class CheckBoxListWithSelectable
extends javax.swing.JList
implements java.awt.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.| Modifier and Type | Class and Description |
|---|---|
protected static class |
CheckBoxListWithSelectable.Handler |
javax.swing.JList.AccessibleJList, javax.swing.JList.DropLocationjavax.swing.JComponent.AccessibleJComponent| Modifier and Type | Field and Description |
|---|---|
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 |
|---|
CheckBoxListWithSelectable()
Constructs a
CheckBoxList with an empty model. |
CheckBoxListWithSelectable(javax.swing.ListModel dataModel)
Constructs a
CheckBoxList that displays the elements in the specified, non-null model. |
CheckBoxListWithSelectable(java.lang.Object[] listData)
Constructs a
CheckBoxList that displays the elements in the specified Object[]. |
CheckBoxListWithSelectable(java.util.Vector<?> listData)
Constructs a
CheckBoxList that displays the elements in the specified Vector. |
| Modifier and Type | Method and Description |
|---|---|
void |
addItemListener(java.awt.event.ItemListener listener)
Adds a listener to the list that's notified each time a change to the item selection occurs.
|
protected CheckBoxListCellRenderer |
createCellRenderer()
Creates the cell renderer.
|
protected CheckBoxListWithSelectable.Handler |
createHandler()
Creates the mouse listener and key listener used by CheckBoxList.
|
protected void |
fireItemStateChanged(java.awt.event.ItemEvent event)
Notifies all listeners that have registered interest for notification on this event type.
|
javax.swing.ListCellRenderer |
getActualCellRenderer() |
javax.swing.ListCellRenderer |
getCellRenderer() |
java.awt.event.ItemListener[] |
getItemListeners()
Returns an array of all the
ItemListeners added to this JList with addItemListener(). |
int |
getNextMatch(java.lang.String prefix,
int startIndex,
javax.swing.text.Position.Bias bias) |
java.lang.Object[] |
getSelectedObjects()
Gets the selected objects.
|
protected void |
init()
Initialize the CheckBoxList.
|
boolean |
isCheckBoxEnabled()
Gets the value of property checkBoxEnabled.
|
boolean |
isCheckBoxVisible(int index)
Checks if check box is visible.
|
boolean |
isClickInCheckBoxOnly()
Gets the value of property clickInCheckBoxOnly.
|
void |
removeItemListener(java.awt.event.ItemListener listener)
Removes a listener from the list that's notified each time a change to the item selection occurs.
|
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 |
setClickInCheckBoxOnly(boolean clickInCheckBoxOnly)
Sets the value of property clickInCheckBoxOnly.
|
void |
setListData(java.lang.Object[] listData) |
void |
setListData(java.util.Vector listData) |
void |
setSelectedObjects(java.lang.Object[] elements)
Sets the selected elements.
|
void |
setSelectedObjects(java.util.Vector<?> objects)
Sets the selected objects.
|
addListSelectionListener, addSelectionInterval, clearSelection, createSelectionModel, ensureIndexIsVisible, fireSelectionValueChanged, getAccessibleContext, getAnchorSelectionIndex, getCellBounds, getDragEnabled, getDropLocation, getDropMode, getFirstVisibleIndex, getFixedCellHeight, getFixedCellWidth, getLastVisibleIndex, getLayoutOrientation, getLeadSelectionIndex, getListSelectionListeners, getMaxSelectionIndex, getMinSelectionIndex, getModel, getPreferredScrollableViewportSize, getPrototypeCellValue, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedIndex, getSelectedIndices, getSelectedValue, getSelectedValues, getSelectedValuesList, getSelectionBackground, getSelectionForeground, getSelectionMode, getSelectionModel, getToolTipText, getUI, getUIClassID, getValueIsAdjusting, getVisibleRowCount, indexToLocation, isSelectedIndex, isSelectionEmpty, locationToIndex, paramString, removeListSelectionListener, removeSelectionInterval, setCellRenderer, setDragEnabled, setDropMode, setFixedCellHeight, setFixedCellWidth, setLayoutOrientation, setModel, setPrototypeCellValue, setSelectedIndex, setSelectedIndices, setSelectedValue, setSelectionBackground, setSelectionForeground, setSelectionInterval, setSelectionMode, setSelectionModel, setUI, setValueIsAdjusting, setVisibleRowCount, updateUIaddAncestorListener, 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, updateadd, 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, validateTreeaction, 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, transferFocusUpCycleprotected CheckBoxListCellRenderer _listCellRenderer
public static final java.lang.String PROPERTY_CHECKBOX_ENABLED
public static final java.lang.String PROPERTY_CLICK_IN_CHECKBOX_ONLY
public CheckBoxListWithSelectable()
CheckBoxList with an empty model.public CheckBoxListWithSelectable(java.util.Vector<?> listData)
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.listData - the Vector to be loaded into the data modelpublic CheckBoxListWithSelectable(java.lang.Object[] listData)
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.listData - the array of Objects to be loaded into the data modelpublic CheckBoxListWithSelectable(javax.swing.ListModel dataModel)
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.dataModel - the data model for this listjava.lang.IllegalArgumentException - if dataModel is nullprotected void init()
protected CheckBoxListCellRenderer createCellRenderer()
protected CheckBoxListWithSelectable.Handler createHandler()
public void setSelectedObjects(java.lang.Object[] elements)
elements - the elements to be selectedpublic void setSelectedObjects(java.util.Vector<?> objects)
objects - the elements to be selected in a Vector.public javax.swing.ListCellRenderer getCellRenderer()
getCellRenderer in class javax.swing.JListpublic javax.swing.ListCellRenderer getActualCellRenderer()
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 void addItemListener(java.awt.event.ItemListener listener)
CheckBoxList will have their ItemEvent.getSource() == this
CheckBoxList.addItemListener in interface java.awt.ItemSelectablelistener - the ItemListener to addpublic void removeItemListener(java.awt.event.ItemListener listener)
removeItemListener in interface java.awt.ItemSelectablelistener - the ItemListener to removepublic java.awt.event.ItemListener[] getItemListeners()
ItemListeners added to this JList with addItemListener().ItemListeners added or an empty array if no listeners have been addedaddItemListener(java.awt.event.ItemListener)protected void fireItemStateChanged(java.awt.event.ItemEvent event)
event parameter.event - the ItemEvent objectEventListenerListpublic java.lang.Object[] getSelectedObjects()
JList.getSelectedValues() which is a JList's feature. The
List returned from this method contains the objects that is checked in the CheckBoxList.getSelectedObjects in interface java.awt.ItemSelectablepublic void selectAll()
public void selectNone()
public void setListData(java.util.Vector listData)
setListData in class javax.swing.JListpublic void setListData(java.lang.Object[] listData)
setListData in class javax.swing.JListpublic int getNextMatch(java.lang.String prefix,
int startIndex,
javax.swing.text.Position.Bias bias)
getNextMatch in class javax.swing.JListpublic boolean isCheckBoxEnabled()
public boolean isCheckBoxVisible(int index)
index - the row index.public void setCheckBoxEnabled(boolean checkBoxEnabled)
checkBoxEnabled - true to enable all the check boxes. False to disable all of them.