Package com.jidesoft.swing
Class CheckBoxListWithSelectable
- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- javax.swing.JComponent
-
- javax.swing.JList
-
- com.jidesoft.swing.CheckBoxListWithSelectable
-
- All Implemented Interfaces:
java.awt.image.ImageObserver,java.awt.ItemSelectable,java.awt.MenuContainer,java.io.Serializable,javax.accessibility.Accessible,javax.swing.Scrollable
public class CheckBoxListWithSelectable extends javax.swing.JList implements java.awt.ItemSelectableCheckBoxListWithSelectableis 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 ofSelectable. If you have your own class that represents the element in the list, you can implementSelectableand 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 toDefaultSelectablewhich implementsSelectableinterface. 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 usingCheckBoxList.getCheckBoxListSelectionModel(). The old implementation kept the selection state at Selectable object in the ListModel.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classCheckBoxListWithSelectable.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 CheckBoxListCellRenderer_listCellRendererstatic 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 CheckBoxListWithSelectable()Constructs aCheckBoxListwith an empty model.CheckBoxListWithSelectable(java.lang.Object[] listData)Constructs aCheckBoxListthat displays the elements in the specifiedObject[].CheckBoxListWithSelectable(java.util.Vector<?> listData)Constructs aCheckBoxListthat displays the elements in the specifiedVector.CheckBoxListWithSelectable(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 voidaddItemListener(java.awt.event.ItemListener listener)Adds a listener to the list that's notified each time a change to the item selection occurs.protected CheckBoxListCellRenderercreateCellRenderer()Creates the cell renderer.protected CheckBoxListWithSelectable.HandlercreateHandler()Creates the mouse listener and key listener used by CheckBoxList.protected voidfireItemStateChanged(java.awt.event.ItemEvent event)Notifies all listeners that have registered interest for notification on this event type.javax.swing.ListCellRenderergetActualCellRenderer()javax.swing.ListCellRenderergetCellRenderer()java.awt.event.ItemListener[]getItemListeners()Returns an array of all theItemListeners added to this JList with addItemListener().intgetNextMatch(java.lang.String prefix, int startIndex, javax.swing.text.Position.Bias bias)java.lang.Object[]getSelectedObjects()Gets the selected objects.protected voidinit()Initialize the CheckBoxList.booleanisCheckBoxEnabled()Gets the value of property checkBoxEnabled.booleanisCheckBoxVisible(int index)Checks if check box is visible.booleanisClickInCheckBoxOnly()Gets the value of property clickInCheckBoxOnly.voidremoveItemListener(java.awt.event.ItemListener listener)Removes a listener from the list that's notified each time a change to the item selection occurs.voidselectAll()Selects all objects in this list except those are disabled.voidselectNone()Deselects all objects in this list except those are disabled.voidsetCheckBoxEnabled(boolean checkBoxEnabled)Sets the value of property checkBoxEnabled.voidsetClickInCheckBoxOnly(boolean clickInCheckBoxOnly)Sets the value of property clickInCheckBoxOnly.voidsetListData(java.lang.Object[] listData)voidsetListData(java.util.Vector listData)voidsetSelectedObjects(java.lang.Object[] elements)Sets the selected elements.voidsetSelectedObjects(java.util.Vector<?> objects)Sets the selected objects.-
Methods inherited from class javax.swing.JList
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, updateUI
-
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
-
-
Constructor Detail
-
CheckBoxListWithSelectable
public CheckBoxListWithSelectable()
Constructs aCheckBoxListwith an empty model.
-
CheckBoxListWithSelectable
public CheckBoxListWithSelectable(java.util.Vector<?> listData)
Constructs aCheckBoxListthat displays the elements in the specifiedVector. If the Vector contains elements which is not an instance ofSelectable, it will wrap it automatically intoDefaultSelectableand add to ListModel.- Parameters:
listData- theVectorto be loaded into the data model
-
CheckBoxListWithSelectable
public CheckBoxListWithSelectable(java.lang.Object[] listData)
Constructs aCheckBoxListthat displays the elements in the specifiedObject[]. If the Object array contains elements which is not an instance ofSelectable, it will wrap it automatically intoDefaultSelectableand add to ListModel.- Parameters:
listData- the array of Objects to be loaded into the data model
-
CheckBoxListWithSelectable
public CheckBoxListWithSelectable(javax.swing.ListModel dataModel)
Constructs aCheckBoxListthat displays the elements in the specified, non-nullmodel. AllCheckBoxListconstructors delegate to this one. Please note, if you are using this constructor, please make sure all elements in dataModel are instance ofSelectable.- Parameters:
dataModel- the data model for this list- Throws:
java.lang.IllegalArgumentException- ifdataModelisnull
-
-
Method Detail
-
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(java.lang.Object[] elements)
Sets the selected elements.- Parameters:
elements- the elements to be selected
-
setSelectedObjects
public void setSelectedObjects(java.util.Vector<?> objects)
Sets the selected objects.- Parameters:
objects- the elements to be selected in a Vector.
-
getCellRenderer
public javax.swing.ListCellRenderer getCellRenderer()
- Overrides:
getCellRendererin classjavax.swing.JList
-
getActualCellRenderer
public javax.swing.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(java.awt.event.ItemListener listener)
Adds a listener to the list that's notified each time a change to the item selection occurs. Listeners added directly to theCheckBoxListwill have theirItemEvent.getSource() == this CheckBoxList.- Specified by:
addItemListenerin interfacejava.awt.ItemSelectable- Parameters:
listener- theItemListenerto add
-
removeItemListener
public 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.- Specified by:
removeItemListenerin interfacejava.awt.ItemSelectable- Parameters:
listener- theItemListenerto remove
-
getItemListeners
public java.awt.event.ItemListener[] getItemListeners()
Returns an array of all theItemListeners added to this JList with addItemListener().- Returns:
- all of the
ItemListeners added or an empty array if no listeners have been added - See Also:
addItemListener(java.awt.event.ItemListener)
-
fireItemStateChanged
protected void fireItemStateChanged(java.awt.event.ItemEvent event)
Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using theeventparameter.- Parameters:
event- theItemEventobject- See Also:
EventListenerList
-
getSelectedObjects
public java.lang.Object[] getSelectedObjects()
Gets the selected objects. This is different fromJList.getSelectedValues()which is a JList's feature. The List returned from this method contains the objects that is checked in the CheckBoxList.- Specified by:
getSelectedObjectsin interfacejava.awt.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(java.util.Vector listData)
- Overrides:
setListDatain classjavax.swing.JList
-
setListData
public void setListData(java.lang.Object[] listData)
- Overrides:
setListDatain classjavax.swing.JList
-
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()
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.
-
-