Lists and Combo Boxes
JLists and
JComboBox
es 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 the user to choose a single selection or to allow
multiple choices. Usually, only a small group of choices are
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. He or 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.
A combo box is a cross-breed between a text field and a list. It displays a single line of text (possibly with an image) and a downward pointing arrow at 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 every other component 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 may 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access