Echo's list selection components, SelectField
and ListBox
, provide a means for a user to select one or more items from a list. Like all non-trivial Echo components, both rely on a model-view-controller pattern to enable them to be efficiently extended.
SelectField
ComponentSelectField
components provide a method of selecting a single item from a list of items. One and only one item will be selected at all times. The selected item is always displayed. Clicking on the drop-down button next to the selected item allows the user to view the entire list of available items and choose a different one if desired.
The state of a SelectField
is represented using a SelectFieldModel
. This model class provides both the list of items to be displayed as well as the selection state of the component. An abstract implementation, AbstractSelectFieldModel
, provides a basic implementation which manages listeners and selection state. The concrete DefaultSelectFieldModel
class serves as the default model. It features the ability to construct its initial state from an array. DefaultSelectFieldModel
also provides methods to dynamically add and remove items to and from itself.
ListBox
ComponentListBox
components allow users to select zero or more items from a list. The component may also be configured to allow at most one item to be selected. ListBoxes display a portion of the selectable items at any given time. The selected item(s) may or may not be visible.
ListBox
es use two different models to represent their state: ListModel
s represent the list of items which may be selected. ListSelectionModel
s represent the selection state of the list, that is, which items are presently selected by the user.
The ListModel
interface specifies a model for items presented in a list box. The abstract class AbstractListModel
provides listener management facilities. DefaultListModel
is a default implementation, which holds the list items as an array. The default implementation also provides methods to add and remove items to and from the model.
The selection model, ListSelectionModel
, is used to describe which items within the list box are selected. A default implementation, DefaultListSelectionModel
, can be used if desired.
When displaying an item returned by a SelectFieldModel
or ListModel
, both list selection components first pass the item through a renderer specified by the interface ListCellRenderer
. The renderer takes the value provided by the model and produces a visually appropriate representation of it. The default renderer, DefaultListCellRenderer
, simply returns the value passed to it.
StyledListCell
interfaceList cell renderers may display items with additional stylistic properties by returning an object that implements the StyledListCell
interface. StyledListCell
is an inner-interface of the ListCellRenderer
interface. A StyledListCell
provides methods which return its desired font and foreground and background colors.