Class ListSubsetField<T>
Sorting: by default, items in both lists maintain their original order as provided in the constructor. You can enable automatic sorting by calling setAutoSortingEnabled(true). When auto-sorting is enabled, items will be sorted in their natural order (assuming T implements Comparable) when items are moved between lists. You can provide a custom Comparator by calling setItemComparator() to control the sort order.
Controlling list size: you can control how many rows are visible in each list by calling setVisibleRowCount(). By default, both lists show 4 rows. You can also specify a fixed cell width by calling setFixedCellWidth(). By default, the fixed cell width is -1, which means the cell width will be automatically sized to fit the largest item in the list. Additionally, you have the option of calling setShouldExpand(true) to have the entire field expand horizontally to fill the available width of the form panel. This defaults to false, meaning the field will be just wide enough to display the two lists at their natural sizes.
Drag and drop: There are two uses of drag and drop within this component:
- Users can drag items from one list to the other to move them between lists.
- When auto-sorting is disabled, users can drag items within a list to reorder the items.
- Since:
- swing-extras 2.6
- Author:
- scorbo2
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intFields inherited from class ca.corbett.forms.fields.FormField
DEFAULT_FONT, defaultFont, extraAttributes, fieldComponent, fieldLabel, fieldValidators, helpLabel, identifier, isEnabled, isVisible, margins, validationLabel, valueChangedListeners -
Constructor Summary
ConstructorsConstructorDescriptionListSubsetField(String label) Creates an empty ListSubsetField with the given field label.ListSubsetField(String label, List<T> availableItems) Creates a ListSubsetField with the given label and list of available items.Creates a ListSubsetField with the given label, list of available items, and list of selected items. -
Method Summary
Modifier and TypeMethodDescriptionClears the current selection - this is equivalent to calling unselectAllItems().Gets the list of currently available items - that is, items that are still present in the left list.ListCellRenderer<? super T> Returns the effective list cell renderer.intReturns the pixel width of each list cell.Returns the comparator used to sort items when they are added to either list.Gets the list of currently selected items - that is, items that are present in the right list.intGets the current visible row count.booleanReturns whether auto-sorting is enabled for both lists.booleanListSubsetFields occupy more than one form row (at least, generally speaking, that statement is true - you could of course set a visibleRowCount of 1, but why would you do that).Allow callers to programmatically move all items right (select all).selectItem(T item) Allow callers to programmatically move an item to the right list (select it).selectItems(List<T> itemsToSelect) Programmatically select the given items.setAutoSortingEnabled(boolean enabled) Sets whether auto-sorting is enabled for both lists.setCellRenderer(ListCellRenderer<T> renderer) You can optionally set a custom cell renderer if your list items have special display requirements.setFixedCellWidth(int width) Sets the pixel width of each list cell.setItemComparator(Comparator<T> comparator) Sets the comparator used to sort items when they are added to either list.setShouldExpand(boolean should) Sets whether this field should expand horizontally to fill available form panel width.setVisibleRowCount(int count) Sets the desired visible row count.booleanOverridden here to allow optional width expansion of the field to fill the form panel's width.Allow callers to programmatically move all items left (unselect all).unselectItem(T item) Allow callers to programmatically move an item to the left list (unselect it).Methods inherited from class ca.corbett.forms.fields.FormField
addAllExtraAttributes, addFieldValidator, addValueChangedListener, clearExtraAttribute, clearExtraAttributes, clearValidationResults, equals, fireValueChangedEvent, getDefaultFont, getExtraAttribute, getFieldComponent, getFieldLabel, getHelpLabel, getHelpText, getIdentifier, getMargins, getValidationLabel, hasFieldLabel, hashCode, hasHelpLabel, hasValidationLabel, isEnabled, isValid, isVisible, preRender, removeAllFieldValidators, removeFieldValidator, removeValueChangedListener, setAllExtraAttributes, setDefaultFont, setEnabled, setExtraAttribute, setFieldLabelFont, setHelpText, setIdentifier, setMargins, setVisible, validate
-
Field Details
-
DEFAULT_VISIBLE_ROW_COUNT
public static final int DEFAULT_VISIBLE_ROW_COUNT- See Also:
-
DEFAULT_FIXED_CELL_WIDTH
public static final int DEFAULT_FIXED_CELL_WIDTH- See Also:
-
-
Constructor Details
-
ListSubsetField
Creates an empty ListSubsetField with the given field label. -
ListSubsetField
Creates a ListSubsetField with the given label and list of available items. Nothing is selected by default. -
ListSubsetField
Creates a ListSubsetField with the given label, list of available items, and list of selected items. The selected items will be removed from the available items, if present. The list of selected items may be empty, and may also reference items that are not present in the available items list. The field's list of items is always the union of the available items and the selected items.
-
-
Method Details
-
getAvailableItems
Gets the list of currently available items - that is, items that are still present in the left list. If the returned list is empty, then all items are selected. -
getSelectedItems
Gets the list of currently selected items - that is, items that are present in the right list. If the returned list is empty, then no items are selected. -
selectItems
Programmatically select the given items. This will only select items that were not already selected. Any items not present in the available items list will be ignored. -
clearSelection
Clears the current selection - this is equivalent to calling unselectAllItems(). -
selectItem
Allow callers to programmatically move an item to the right list (select it). Does nothing if the given item is not present in the available items list. -
unselectItem
Allow callers to programmatically move an item to the left list (unselect it). Does nothing if the given item is not present in the selected items list. -
selectAllItems
Allow callers to programmatically move all items right (select all). -
unselectAllItems
Allow callers to programmatically move all items left (unselect all). -
getVisibleRowCount
public int getVisibleRowCount()Gets the current visible row count. The default count is 4. Internally, we return the visible row count for the available list, but the two lists are kept in sync, so they will always be the same. -
setVisibleRowCount
Sets the desired visible row count. The default count is 4. Both the available list and the selected list will be set to this value. -
setCellRenderer
You can optionally set a custom cell renderer if your list items have special display requirements. Both the available list and the selected list will be set to use this renderer. -
getCellRenderer
Returns the effective list cell renderer. Internally, we return the renderer for the available list, but the two lists are kept in sync, so both lists use the same renderer. -
getItemComparator
Returns the comparator used to sort items when they are added to either list. If null, the natural ordering of the items is used. The default value is null. -
setItemComparator
Sets the comparator used to sort items when they are added to either list. If null, the natural ordering of the items is used. -
isAutoSortingEnabled
public boolean isAutoSortingEnabled()Returns whether auto-sorting is enabled for both lists. When enabled, items are sorted when moved between lists. When disabled (default), items maintain their original order. -
setAutoSortingEnabled
Sets whether auto-sorting is enabled for both lists. When enabled, items are sorted when moved between lists. When disabled (default), items maintain their original order.Note: Enabling auto-sorting will immediately sort both lists. Disabling auto-sorting will not change the current ordering of items.
Note: Enabling auto-sorting will automatically disable the ability to drag items within a list to re-order the list. When auto-sorting is disabled, users can drag items within a list to change their order.
-
getFixedCellWidth
public int getFixedCellWidth()Returns the pixel width of each list cell. A value of -1 here means the list cells will auto-size their widths based on the width of the longest item in the list. Internally, we return the fixed cell width for the available list, but the two lists are kept in sync, so both lists have the same fixed cell width. The default value is -1. -
setFixedCellWidth
Sets the pixel width of each list cell. The default value is -1, which will set each cell's width to the width of the largest item. Both the available list and the selected list will be set to this value. -
isMultiLine
public boolean isMultiLine()ListSubsetFields occupy more than one form row (at least, generally speaking, that statement is true - you could of course set a visibleRowCount of 1, but why would you do that).- Overrides:
isMultiLinein classFormField
-
setShouldExpand
Sets whether this field should expand horizontally to fill available form panel width. The default is false, meaning the field will be just wide enough to display the two lists at their natural sizes. -
shouldExpand
public boolean shouldExpand()Overridden here to allow optional width expansion of the field to fill the form panel's width. The default is false.- Overrides:
shouldExpandin classFormField
-