Package ca.corbett.forms.fields
Class ComboField<T>
java.lang.Object
ca.corbett.forms.fields.FormField
ca.corbett.forms.fields.ComboField<T>
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
-
Field Summary
Fields 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
ConstructorsConstructorDescriptionComboField(String label) ComboField(String label, List<T> options, int selectedIndex) ComboField(String label, List<T> options, int selectedIndex, boolean isEditable) Creates a new ComboField with the given parameters. -
Method Summary
Modifier and TypeMethodDescriptionListCellRenderer<? super T> Returns the effective cell renderer.Exposes the underlying DefaultComboBoxModel if you need to add or remove elements after the ComboField has been instantiated.intReturns the number of items currently in the combo box.intReturns the index of the currently selected item.Returns the currently selected item as an instance of T.booleanOverridden here as we generally don't want to show a validation label on a combo box.setCellRenderer(ListCellRenderer<T> renderer) You can optionally set a custom cell renderer if your combo items have special display requirements.setOptions(List<T> options, int selectedIndex) Sets the available options in this field, overwriting whatever options were there before.setSelectedIndex(int index) Sets the selected item index.setSelectedItem(T item) Sets the selected item.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, isEnabled, isMultiLine, isValid, isVisible, preRender, removeAllFieldValidators, removeFieldValidator, removeValueChangedListener, setAllExtraAttributes, setDefaultFont, setEnabled, setExtraAttribute, setFieldLabelFont, setHelpText, setIdentifier, setMargins, setVisible, shouldExpand, validate
-
Constructor Details
-
ComboField
-
ComboField
-
ComboField
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:
hasValidationLabelin classFormField
-
setOptions
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()Returns the number of items currently in the combo box. -
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
Sets the selected item. This is simply a passthrough to JComboBox.- Parameters:
item- The item to select.
-
setSelectedIndex
Sets the selected item index. This is simply a passthrough to JComboBox.- Parameters:
index- The index to select.
-
setCellRenderer
You can optionally set a custom cell renderer if your combo items have special display requirements. -
getCellRenderer
Returns the effective cell renderer. -
getComboModel
Exposes the underlying DefaultComboBoxModel if you need to add or remove elements after the ComboField has been instantiated.
-