Java Reference
In-Depth Information
CHAPTER 14
■ ■ ■
Spinner Model Controls
I
n the previous chapter, you learned how to work with the basic list controls:
JList
and
JComboBox
. In this chapter, you will move on to the
JSpinner
component, introduced with
the 1.4 version of the JDK.
JSpinner Class
The
JSpinner
works like a cross between a
JList
or
JComboBox
component with a
JFormattedTextField
. In either the
JList
and
JComboBox
control, the user can select input from
a predetermined set of values. The
JSpinner
also allows this type of selection. The other half of
the component is the
JFormattedTextField
. How to display or enter the value isn't controlled
by a list cell renderer, as in a
JList
; instead, you get a
JFormattedTextField
for entry and a
couple of arrows on the side to navigate through the different values available for the text field.
(The
JFormattedTextField
is covered in Chapters 15 and 16.)
Figure 14-1 shows what the spinner looks like for several different types of input. At the top
of Figure 14-1 is a
JSpinner
with the days of the week in French provided to a
SpinnerListModel
.
In the middle, you have a
JSpinner
for a date via the
SpinnerDateModel
class. On the bottom is
the
JSpinner
usage with the
SpinnerNumberModel
. Each of these three work in its own myste-
rious way, as you'll learn later in this chapter.
Figure 14-1.
JSpinner examples
Many classes are involved when creating and manipulating
JSpinner
components, foremost
the
JSpinner
class itself. The primary two sets of classes involved are the
SpinnerModel
interface,
for containing the set of selectable items for the control, and, the
JSpinner.DefaultEditor
implementations, for catching all the selections. Thankfully, many of the other classes
involved work behind the scenes, so, for example, once you provide the numeric range in a
SpinnerNumberModel
and associate the spinner with its model, your work is essentially done.
509