Package com.jidesoft.swing
Class CheckBoxList
- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- javax.swing.JComponent
-
- javax.swing.JList
-
- com.jidesoft.swing.CheckBoxList
-
- All Implemented Interfaces:
java.awt.image.ImageObserver,java.awt.MenuContainer,java.io.Serializable,javax.accessibility.Accessible,javax.swing.Scrollable
public class CheckBoxList extends javax.swing.JListCheckBoxListis 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 usingJList.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 usinggetCheckBoxListSelectionModel(). 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 toCheckBoxListWithSelectable. 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 usinggetCheckBoxListSelectionModel(). 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 ofCheckBoxTree.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classCheckBoxList.Handler-
Nested classes/interfaces inherited from class javax.swing.JList
javax.swing.JList.AccessibleJList, javax.swing.JList.DropLocation
-
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
-
-
Field Summary
Fields Modifier and Type Field Description protected CheckBoxList.Handler_handlerprotected CheckBoxListCellRenderer_listCellRendererstatic java.lang.StringALLDeprecated.replaced byALL_ENTRYstatic java.lang.ObjectALL_ENTRYThe default all entry for CheckBoxList.static java.lang.StringPROPERTY_CHECKBOX_ENABLEDstatic java.lang.StringPROPERTY_CLICK_IN_CHECKBOX_ONLY-
Fields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
-
-
Constructor Summary
Constructors Constructor Description CheckBoxList()Constructs aCheckBoxListwith an empty model.CheckBoxList(java.lang.Object[] listData)Constructs aCheckBoxListthat displays the elements in the specifiedObject[].CheckBoxList(java.util.Vector<?> listData)Constructs aCheckBoxListthat displays the elements in the specifiedVector.CheckBoxList(javax.swing.ListModel dataModel)Constructs aCheckBoxListthat displays the elements in the specified, non-nullmodel.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddCheckBoxListSelectedIndex(int index)Selects a single cell and keeps all previous selections.voidaddCheckBoxListSelectedValue(java.lang.Object anObject, boolean shouldScroll)Selects the specified object from the list and keep all previous selections.voidaddCheckBoxListSelectedValues(java.lang.Object[] objects)Selects the specified objects from the list and keep all previous selections.voidclearCheckBoxListSelection()protected CheckBoxListCellRenderercreateCellRenderer()Creates the cell renderer.protected CheckBoxListSelectionModelcreateCheckBoxListSelectionModel(javax.swing.ListModel model)protected CheckBoxList.HandlercreateHandler()Creates the mouse listener and key listener used by CheckBoxList.javax.swing.ListCellRenderergetActualCellRenderer()javax.swing.ListCellRenderergetCellRenderer()intgetCheckBoxListSelectedIndex()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.ObjectgetCheckBoxListSelectedValue()Returns the first selected value, ornullif the selection is empty.java.lang.Object[]getCheckBoxListSelectedValues()Returns an array of the values for the selected cells.CheckBoxListSelectionModelgetCheckBoxListSelectionModel()Gets the ListSelectionModel that keeps the check boxes' state information for CheckBoxList.intgetLastVisibleIndex()intgetNextMatch(java.lang.String prefix, int startIndex, javax.swing.text.Position.Bias bias)java.awt.DimensiongetPreferredScrollableViewportSize()protected voidinit()Initialize the CheckBoxList.booleanisCheckBoxEnabled()Gets the value of property checkBoxEnabled.booleanisCheckBoxEnabled(int index)Checks if check box is enabled.booleanisCheckBoxVisible(int index)Checks if check box is visible.booleanisClickInCheckBoxOnly()Gets the value of property clickInCheckBoxOnly.voidremoveCheckBoxListSelectedIndex(int index)Deselects a single cell.voidremoveCheckBoxListSelectedValue(java.lang.Object anObject, boolean shouldScroll)Deselects the specified object from the list.voidremoveCheckBoxListSelectedValues(java.lang.Object[] objects)Deselects the specified objects from the list and keep all previous selections.voidselectAll()Selects all objects in this list.voidselectNone()Deselects all objects in this list.voidsetCheckBoxEnabled(boolean checkBoxEnabled)Sets the value of property checkBoxEnabled.voidsetCheckBoxListSelectedIndex(int index)Selects a single cell and clear all other selections.voidsetCheckBoxListSelectedIndices(int[] indices)Selects a set of cells.voidsetCheckBoxListSelectedValue(java.lang.Object anObject, boolean shouldScroll)Selects the specified object from the list and clear all other selections.voidsetCheckBoxListSelectionModel(CheckBoxListSelectionModel checkBoxListSelectionModel)voidsetClickInCheckBoxOnly(boolean clickInCheckBoxOnly)Sets the value of property clickInCheckBoxOnly.voidsetModel(javax.swing.ListModel model)voidsetSelectedObjects(java.lang.Object[] elements)Sets the selected elements.voidsetSelectedObjects(java.util.Vector<?> elements)Sets the selected elements.voidupdateUI()-
Methods inherited from class javax.swing.JList
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, getUIClassID, getValueIsAdjusting, getVisibleRowCount, indexToLocation, isSelectedIndex, isSelectionEmpty, locationToIndex, paramString, removeListSelectionListener, removeSelectionInterval, setCellRenderer, setDragEnabled, setDropMode, setFixedCellHeight, setFixedCellWidth, setLayoutOrientation, setListData, setListData, setPrototypeCellValue, setSelectedIndex, setSelectedIndices, setSelectedValue, setSelectionBackground, setSelectionForeground, setSelectionInterval, setSelectionMode, setSelectionModel, setUI, setValueIsAdjusting, setVisibleRowCount
-
Methods inherited from class javax.swing.JComponent
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
-
Methods inherited from class java.awt.Container
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
-
Methods inherited from class java.awt.Component
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, 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, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
-
-
-
Field Detail
-
_listCellRenderer
protected CheckBoxListCellRenderer _listCellRenderer
-
PROPERTY_CHECKBOX_ENABLED
public static final java.lang.String PROPERTY_CHECKBOX_ENABLED
- See Also:
- Constant Field Values
-
PROPERTY_CLICK_IN_CHECKBOX_ONLY
public static final java.lang.String PROPERTY_CLICK_IN_CHECKBOX_ONLY
- See Also:
- Constant Field Values
-
_handler
protected CheckBoxList.Handler _handler
-
ALL
@Deprecated public static final java.lang.String ALL
Deprecated.replaced byALL_ENTRY- See Also:
- Constant Field Values
-
ALL_ENTRY
public static final java.lang.Object ALL_ENTRY
The default all entry for CheckBoxList.- Since:
- 3.4.1
-
-
Constructor Detail
-
CheckBoxList
public CheckBoxList()
Constructs aCheckBoxListwith an empty model.
-
CheckBoxList
public CheckBoxList(java.util.Vector<?> listData)
Constructs aCheckBoxListthat displays the elements in the specifiedVector.- Parameters:
listData- theVectorto be loaded into the data model
-
CheckBoxList
public CheckBoxList(java.lang.Object[] listData)
Constructs aCheckBoxListthat displays the elements in the specifiedObject[].- Parameters:
listData- the array of Objects to be loaded into the data model
-
CheckBoxList
public CheckBoxList(javax.swing.ListModel dataModel)
Constructs aCheckBoxListthat displays the elements in the specified, non-nullmodel. AllCheckBoxListconstructors delegate to this one.- Parameters:
dataModel- the data model for this list- Throws:
java.lang.IllegalArgumentException- ifdataModelisnull
-
-
Method Detail
-
setModel
public void setModel(javax.swing.ListModel model)
- Overrides:
setModelin classjavax.swing.JList
-
updateUI
public void updateUI()
- Overrides:
updateUIin classjavax.swing.JList
-
init
protected void init()
Initialize the CheckBoxList.
-
getLastVisibleIndex
public int getLastVisibleIndex()
- Overrides:
getLastVisibleIndexin classjavax.swing.JList
-
createCheckBoxListSelectionModel
protected CheckBoxListSelectionModel createCheckBoxListSelectionModel(javax.swing.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 javax.swing.ListCellRenderer getCellRenderer()
- Overrides:
getCellRendererin classjavax.swing.JList
-
getActualCellRenderer
public javax.swing.ListCellRenderer getActualCellRenderer()
-
getNextMatch
public int getNextMatch(java.lang.String prefix, int startIndex, javax.swing.text.Position.Bias bias)- Overrides:
getNextMatchin classjavax.swing.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:
JList.removeSelectionInterval(int, int),JList.addListSelectionListener(javax.swing.event.ListSelectionListener)
-
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:
ListSelectionModel.setSelectionInterval(int, int),JList.isSelectedIndex(int),JList.addListSelectionListener(javax.swing.event.ListSelectionListener)
-
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:
ListSelectionModel.setSelectionInterval(int, int),JList.isSelectedIndex(int),JList.addListSelectionListener(javax.swing.event.ListSelectionListener)
-
removeCheckBoxListSelectedIndex
public void removeCheckBoxListSelectedIndex(int index)
Deselects a single cell.- Parameters:
index- the index of the one cell to select- See Also:
ListSelectionModel.setSelectionInterval(int, int),JList.isSelectedIndex(int),JList.addListSelectionListener(javax.swing.event.ListSelectionListener)
-
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:
ListSelectionModel.addSelectionInterval(int, int),JList.isSelectedIndex(int),JList.addListSelectionListener(javax.swing.event.ListSelectionListener)
-
setSelectedObjects
public void setSelectedObjects(java.lang.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(java.util.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 java.lang.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:
JList.isSelectedIndex(int),JList.getModel(),JList.addListSelectionListener(javax.swing.event.ListSelectionListener)
-
getCheckBoxListSelectedIndex
public int getCheckBoxListSelectedIndex()
Returns the first selected index; returns -1 if there is no selected item.- Returns:
- the value of
getMinSelectionIndex - See Also:
JList.getMinSelectionIndex(),JList.addListSelectionListener(javax.swing.event.ListSelectionListener)
-
getCheckBoxListSelectedValue
public java.lang.Object getCheckBoxListSelectedValue()
Returns the first selected value, ornullif the selection is empty.- Returns:
- the first selected value
- See Also:
JList.getMinSelectionIndex(),JList.getModel(),JList.addListSelectionListener(javax.swing.event.ListSelectionListener)
-
setCheckBoxListSelectedValue
public void setCheckBoxListSelectedValue(java.lang.Object anObject, boolean shouldScroll)Selects the specified object from the list and clear all other selections.- Parameters:
anObject- the object to selectshouldScroll- true if the list should scroll to display the selected object, if one exists; otherwise false
-
addCheckBoxListSelectedValue
public void addCheckBoxListSelectedValue(java.lang.Object anObject, boolean shouldScroll)Selects the specified object from the list and keep all previous selections.- Parameters:
anObject- the object to be selectedshouldScroll- true if the list should scroll to display the selected object, if one exists; otherwise false
-
addCheckBoxListSelectedValues
public void addCheckBoxListSelectedValues(java.lang.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(java.lang.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(java.lang.Object anObject, boolean shouldScroll)Deselects the specified object from the list.- Parameters:
anObject- the object to selectshouldScroll- 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 java.awt.Dimension getPreferredScrollableViewportSize()
- Specified by:
getPreferredScrollableViewportSizein interfacejavax.swing.Scrollable- Overrides:
getPreferredScrollableViewportSizein classjavax.swing.JList
-
-