Class ComboField<T>

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

public class ComboField<T> extends FormField
A FormField wrapping a JComboBox. The field is typed to a specific class - the usual case is to use ComboField<String> to wrap a simple list of Strings, but you can use your own class type here as long as it has a meaningful toString().

Getting access to the underlying JComboBox - if you need access to the underlying JComboBox, for example for styling purposes, to set a different font, etc, you can use getFieldComponent() and cast the result to JComboBox with whatever type you used with this class (for example, JComboBox<String>)

Since:
2019-11-24
Author:
scorbo2
  • Constructor Details

    • ComboField

      public ComboField(String label)
    • ComboField

      public ComboField(String label, List<T> options, int selectedIndex)
    • ComboField

      public ComboField(String label, List<T> options, int selectedIndex, boolean isEditable)
      Creates a new ComboField with the given parameters.
      Parameters:
      label - The label to use with this field.
      options - The options to display in the dropdown.
      selectedIndex - The index to select by default.
      isEditable - Whether to allow editing of the field.
  • Method Details

    • hasValidationLabel

      public boolean hasValidationLabel()
      Overridden here as we generally don't want to show a validation label on a combo box. Will return true only if one or more FieldValidators have been explicitly assigned.
      Overrides:
      hasValidationLabel in class FormField
    • setOptions

      public ComboField<T> setOptions(List<T> options, int selectedIndex)
      Sets the available options in this field, overwriting whatever options were there before.
      Parameters:
      options - The options to display in the dropdown.
      selectedIndex - The index to select by default.
    • getItemCount

      public int getItemCount()
    • getSelectedItem

      public T getSelectedItem()
      Returns the currently selected item as an instance of T.
    • getSelectedIndex

      public int getSelectedIndex()
      Returns the index of the currently selected item.
      Returns:
      The index of the currently selected item.
    • setSelectedItem

      public ComboField<T> setSelectedItem(T item)
      Sets the selected item. This is simply a passthrough to JComboBox.
      Parameters:
      item - The item to select.
    • setSelectedIndex

      public ComboField<T> setSelectedIndex(int index)
      Sets the selected item index. This is simply a passthrough to JComboBox.
      Parameters:
      index - The index to select.
    • setCellRenderer

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

      public ListCellRenderer<? super T> getCellRenderer()
      Returns the effective cell renderer.