The Spinner
JList and JComboBox are two ways to let the user choose
from a set of values. A JComboBox has
added flexibility when it is made editable, but, in general, both of these
components are limited in that they can only prompt the user from a fixed
set of choices. In Java 1.4, Swing added a component called JSpinner that is useful for large or open-ended
sequences of values such as numbers or dates. The JSpinner is a cousin of the JComboBox; it displays a value in a field, but
instead of providing a drop-down list of choices, it gives the user a
small pair of up and down arrows for moving over a range of values (see
Figure 17-5). Like the combo box, a
JSpinner can also be made editable,
allowing the user to type a valid value directly into the field.

Figure 17-5. Image of DateSelector application
Swing provides three basic types of Spinners, represented by three different data
models for the JSpinner component:
SpinnerListModel,
SpinnerNumberModel, and
SpinnerDateModel.
The SpinnerListModel acts like a
combo box, specifying a fixed set of objects:
String[]options=newString[]{"small","medium","large","huge"};SpinnerListModelmodel=newSpinnerListModel(options);JSpinnerspinner=newJSpinner(model);
You can retrieve the current value from the model at any time:
Stringvalue=(String)model.getValue();
Alternatively, you can register a ChangeListener to receive ...
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