Java Reference
In-Depth Information
CHAPTER 13
■ ■ ■
List Model Controls
C
hapter 12 explored the bounded range controls that support scrolling and the input or
display of some bounded range of values. In this chapter, you'll examine two data-selection
controls that present a list of choices:
JList
and
JComboBox
. The primary difference between
these controls is that the
JList
component supports multiple selections, whereas the
JComboBox
does not. Also, the
JComboBox
lets a user provide a choice that isn't among the available options.
ListModel Interface
Figure 13-1 shows the two controls you'll be examining in this chapter.
Figure 13-1.
Sample JComboBox and JList controls
The data model shared by the two components is
ListModel
, which originates with the
ListModel
interface. The
AbstractListModel
class provides an implementation basis by supporting
the management and notification of a set of
ListDataListener
objects.
In the case of a
JList
component, the data model implementation is the
DefaultListModel
class. This class adds an actual data repository, which follows the API of a
Vector
, for the different
elements to be displayed within the
JList
component.
In the
JComboBox
component, an extension of the
ListModel
interface called
ComboBoxModel
supports the notion of a selected item within the model. The
DefaultComboBoxModel
class
implements the
ComboBoxModel
interface through yet another interface, the
MutableComboBoxModel
,
which supplies supporting methods for adding and removing elements from the model.
451