Lists and Combo Boxes
JLists and JComboBoxes are a step up on the evolutionary
chain from JButtons and JLabels. Lists let the user choose from a group
of alternatives. They can be configured to force a single selection or
allow multiple choices. Usually, only a small group of choices is
displayed at a time; a scrollbar lets the user move to the choices that
aren’t visible. The user can select an item by clicking on it. She can
expand the selection to a range of items by holding down Shift and
clicking on another item. To make discontinuous selections, the user can
hold down the Control key instead of the Shift key (on a Mac, this is the
Command key).
A combo box is a crossbreed between a text field and a list. It displays a single line of text (possibly with an image) and a downward-pointing arrow on one side. If you click on the arrow, the combo box opens up and displays a list of choices. You can select a single choice by clicking on it. After a selection is made, the combo box closes up; the list disappears, and the new selection is shown in the text field.
Like other components in Swing, lists and combo boxes have data models that are distinct from visual components. The list also has a selection model that controls how selections can be made on the list data.
Lists and combo boxes are similar because they have similar data
models. Each is simply an array of acceptable choices. This similarity is
reflected in Swing, of course: the type of a JComboBox’s data model is a subclass ...