Class ListField<T>
An important note about value changed events - this field's "value" is the list of selected items. Therefore, value changed events are only fired when the selection changes, and NOT when the list contents change. If you need to be notified when the list contents change, you will need to add a ListDataListener to the underlying ListModel yourself. You can do this by accessing the list model directly:
myListField.getListModel().addListDataListener(...);
Or, you can use the addListDataListener() convenience method:
myListField.addListDataListener(...);
Adding action buttons to a ListField
You can add action buttons to the ListField by using the addButton(Action) method. This works very similarly to ButtonField, in that you can control the alignment, hgap, and vgap of the button panel. ListField offers the option of placing the button panel either below the list (by default) or above the list, via the setButtonPosition() method. You can control the preferred size of buttons added to the button panel via the setButtonPreferredSize() method.
There are some built-in actions included with swing-extras that are useful for ListFields, such as ListItemRemoveAction, ListItemMoveAction, and ListItemClearAction. These are found in the ca.corbett.forms.actions package. Adding your own actions is very easy - you can use any Action instance, but you should consider extending the EnhancedAction class to make setting things like icons and tooltips easier. See the example actions in the built-in demo application on the "Forms: lists and panels" tab!
- Since:
- swing-extras 2.3
- Author:
- scorbo2
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intstatic final ListField.ButtonPositionstatic final intFields inherited from class ca.corbett.forms.fields.FormField
DEFAULT_FONT, defaultFont, extraAttributes, fieldComponent, fieldLabel, fieldValidators, helpLabel, ICON_SIZE, identifier, isEnabled, isVisible, margins, validationLabel, valueChangedListeners -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds an Action to the button panel.addListDataListener(ListDataListener listener) A convenience method to add a ListDataListener to the underlying ListModel.protected static <T> DefaultListModel<T> createDefaultListModel(List<T> items) intOne of FlowLayout's alignment options: LEFT, CENTER, RIGHT, LEADING, or TRAILING from the FlowLayout class.intGets the horizontal gap between buttons.Gets the position of the button panel (above or below the list).Gets the preferred size of buttons in the button panel, or null if buttons should size to their content.intGets the vertical gap between the list and the button panel.ListCellRenderer<? super T> Returns the effective list cell renderer.intReturns the pixel width of each list cell.intGets the layout orientation (allowable values are JList.VERTICAL, JList.VERTICAL_WRAP, and JList.HORIZONTAL_WRAP).getList()Provides direct access to the underlying JList if needed.Provides direct access to the underlying ListModel.int[]Returns an array of selected item indexes.intGets the list selection model (allowable values are ListSelectionModel.SINGLE_SELECTION, ListSelectionModel.MULTIPLE_INTERVAL_SELECTION, and ListSelectionModel.SINGLE_INTERVAL_SELECTION).intbooleanListFields occupy more than one form row (generally - you can of course set a visibleRowCount of 1, but why would you do that).removeListDataListener(ListDataListener listener) Convenience method to remove a ListDataListener from the underlying ListModel.setButtonAlignment(int alignment) One of FlowLayout's alignment options: LEFT, CENTER, RIGHT, LEADING, or TRAILING from the FlowLayout class.setButtonHgap(int hgap) Sets the horizontal gap between buttons, and between buttons and the edge of the containing panel.setButtonLayout(int alignment, int hgap, int vgap) A convenience method to set alignment, hgap, and vgap all at once.setButtonPanelBorder(Border border) Allows callers to set a border on the button panel.setButtonPosition(ListField.ButtonPosition position) Sets the position of the button panel (above or below the list).Allow changing the preferred size of buttons in the button panel.setButtonVgap(int vgap) Sets the vertical gap between buttons, and between buttons and the edge of the containing panel.setCellRenderer(ListCellRenderer<T> renderer) You can optionally set a custom cell renderer if your list items have special display requirements.setEnabled(boolean isEnabled) Enables or disables all components in this field.setFixedCellWidth(int width) Sets the pixel width of each list cell.setLayoutOrientation(int orientation) Sets the layout orientation (allowable values are JList.VERTICAL, JList.VERTICAL_WRAP, and JList.HORIZONTAL_WRAP).setSelectedIndex(int index) Sets a specific list index to select.setSelectedIndexes(int[] selection) Sets the selected indexes for the list.setSelectionMode(int selectionMode) Sets the list selection model (allowable values are ListSelectionModel.SINGLE_SELECTION, ListSelectionModel.MULTIPLE_INTERVAL_SELECTION, and ListSelectionModel.SINGLE_INTERVAL_SELECTION).voidsetShouldExpand(boolean should) Sets whether this list 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 list to fill the form panel's width.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, setExtraAttribute, setFieldLabelFont, setHelpText, setIdentifier, setMargins, setVisible, validate
-
Field Details
-
DEFAULT_BUTTON_POSITION
-
DEFAULT_BUTTON_ALIGNMENT
public static final int DEFAULT_BUTTON_ALIGNMENT- See Also:
-
DEFAULT_BUTTON_HGAP
public static final int DEFAULT_BUTTON_HGAP- See Also:
-
DEFAULT_BUTTON_VGAP
public static final int DEFAULT_BUTTON_VGAP- See Also:
-
-
Constructor Details
-
ListField
If you supply a List of T, we will create and use a DefaultListModel implicitly. Use the overloaded constructor if you wish to use your own ListModel instead. -
ListField
If you supply a ListModel of T, we will use that instead of creating a DefaultListModel. Either way, you can retrieve the model after creation with the getListModel() method.
-
-
Method Details
-
getSelectionMode
public int getSelectionMode()Gets the list selection model (allowable values are ListSelectionModel.SINGLE_SELECTION, ListSelectionModel.MULTIPLE_INTERVAL_SELECTION, and ListSelectionModel.SINGLE_INTERVAL_SELECTION). -
setSelectionMode
Sets the list selection model (allowable values are ListSelectionModel.SINGLE_SELECTION, ListSelectionModel.MULTIPLE_INTERVAL_SELECTION, and ListSelectionModel.SINGLE_INTERVAL_SELECTION). Any other value is ignored. The default value is MULTIPLE_INTERVAL_SELECTION. -
getLayoutOrientation
public int getLayoutOrientation()Gets the layout orientation (allowable values are JList.VERTICAL, JList.VERTICAL_WRAP, and JList.HORIZONTAL_WRAP). -
setLayoutOrientation
Sets the layout orientation (allowable values are JList.VERTICAL, JList.VERTICAL_WRAP, and JList.HORIZONTAL_WRAP). Any other value is ignored. The default value is VERTICAL. -
setEnabled
Description copied from class:FormFieldEnables or disables all components in this field.Note: The help label is intentionally not disabled when the field is disabled. This is because the help label should always be visible and accessible to the user, even when the field itself is disabled. Descendant classes can override
setEnabled()to implement their own handling if needed.- Overrides:
setEnabledin classFormField- Parameters:
isEnabled- whether to enable or disable the components.
-
getVisibleRowCount
public int getVisibleRowCount() -
setVisibleRowCount
Sets the desired visible row count. The default count is 4. -
getSelectedIndexes
public int[] getSelectedIndexes()Returns an array of selected item indexes. -
setSelectedIndexes
Sets the selected indexes for the list. Note: this may not do what you expect it to do depending on the current selection mode (default selection mode is MULTIPLE_INTERVAL_SELECTION, which allows multiple non-contiguous items to be selected).Passing null or an empty list will clear the selection.
-
setSelectedIndex
Sets a specific list index to select. If out of bounds, this call is ignored. -
getList
Provides direct access to the underlying JList if needed. -
getListModel
Provides direct access to the underlying ListModel. By default (unless the constructor was given something else), this will return a DefaultListModel<T> instance. -
addListDataListener
A convenience method to add a ListDataListener to the underlying ListModel. Important: If you add a ValueChangedListener to this ListField, that listener will only be notified when the selection changes, not when the list data changes. If you need to be notified when the list data changes, you must add a ListDataListener to the ListModel. This method makes that a bit easier. -
removeListDataListener
Convenience method to remove a ListDataListener from the underlying ListModel. -
setCellRenderer
You can optionally set a custom cell renderer if your list items have special display requirements. -
getCellRenderer
Returns the effective list cell renderer. -
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. -
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. -
setShouldExpand
public void setShouldExpand(boolean should) Sets whether this list should expand horizontally to fill available form panel width. The default is false, meaning the list will be just wide enough to display the longest item. -
isMultiLine
public boolean isMultiLine()ListFields occupy more than one form row (generally - you can of course set a visibleRowCount of 1, but why would you do that).- Overrides:
isMultiLinein classFormField
-
shouldExpand
public boolean shouldExpand()Overridden here to allow optional width expansion of the list to fill the form panel's width. The default is false.- Overrides:
shouldExpandin classFormField
-
getButtonPosition
Gets the position of the button panel (above or below the list). -
setButtonPosition
Sets the position of the button panel (above or below the list). -
addButton
Adds an Action to the button panel. This will cause the button panel to become visible if it was not already. -
setButtonPanelBorder
Allows callers to set a border on the button panel. The default is no border. -
getButtonPreferredSize
Gets the preferred size of buttons in the button panel, or null if buttons should size to their content. -
setButtonPreferredSize
Allow changing the preferred size of buttons in the button panel. By default, buttons will size to their content. -
getButtonAlignment
public int getButtonAlignment()One of FlowLayout's alignment options: LEFT, CENTER, RIGHT, LEADING, or TRAILING from the FlowLayout class. -
setButtonAlignment
One of FlowLayout's alignment options: LEFT, CENTER, RIGHT, LEADING, or TRAILING from the FlowLayout class. -
getButtonHgap
public int getButtonHgap()Gets the horizontal gap between buttons. -
getButtonVgap
public int getButtonVgap()Gets the vertical gap between the list and the button panel. -
setButtonHgap
Sets the horizontal gap between buttons, and between buttons and the edge of the containing panel. -
setButtonVgap
Sets the vertical gap between buttons, and between buttons and the edge of the containing panel. -
setButtonLayout
A convenience method to set alignment, hgap, and vgap all at once.- Parameters:
alignment- One of the FlowLayout alignment options: LEFT, CENTER, RIGHT, LEADING, or TRAILINGhgap- The horizontal gap between buttons, in pixelsvgap- The vertical gap between the buttons and the list, in pixels- Returns:
- this ListField instance, for call chaining
-
createDefaultListModel
-