Class ListField<T>

java.lang.Object
ca.corbett.forms.fields.FormField
ca.corbett.forms.fields.ListField<T>

public class ListField<T> extends FormField
Wraps a JList to allow for multi-selection of some object type. A common use case would be ListField<String> to wrap a simple list of Strings. The underlying JList can be obtained by calling getList(), if you need to do custom styling or whatnot on the JList.
Since:
swing-extras 2.3
Author:
scorbo2
  • Constructor Details

    • ListField

      public ListField(String label, List<T> items)
      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

      public ListField(String label, AbstractListModel<T> listModel)
      If you supply a ListModel of T, we will use that instead of creating a DefaultListModel, but beware - you must populate the model beforehand.
  • 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

      public ListField<T> setSelectionMode(int selectionMode)
      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

      public ListField<T> setLayoutOrientation(int orientation)
      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.
    • getVisibleRowCount

      public int getVisibleRowCount()
    • setVisibleRowCount

      public ListField<T> setVisibleRowCount(int count)
      Sets the desired visible row count. The default count is 4.
    • getSelectedIndexes

      public int[] getSelectedIndexes()
      Returns an array of selected item indexes.
    • setSelectedIndexes

      public ListField<T> setSelectedIndexes(int[] selection)
      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

      public ListField<T> setSelectedIndex(int index)
      Sets a specific list index to select. If out of bounds, this call is ignored.
    • getList

      public JList<T> getList()
      Provides direct access to the underlying JList if needed.
    • getListModel

      public AbstractListModel<T> getListModel()
      Provides direct access to the underlying ListModel. By default (unless the constructor was given something else), this will return a DefaultListModel<T> instance.
    • setCellRenderer

      public ListField<T> setCellRenderer(ListCellRenderer<T> renderer)
      You can optionally set a custom cell renderer if your list items have special display requirements.
    • getCellRenderer

      public ListCellRenderer<? super T> 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

      public ListField<T> setFixedCellWidth(int width)
      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:
      isMultiLine in class FormField
    • 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:
      shouldExpand in class FormField