How to Create a Spinner Control in Java

By Doug Lowe

A spinner is a text field you can create in Java that has two little arrows next to it. The user can click one of these arrows to increase or decrease the text field value. Usually the text field contains a number, so clicking one of the little arrows increments or decrements the number. You can also create a spinner that displays data taken from an array or a collection.

image0.jpg

To create a spinner control, you use the JSpinner class. Check out the constructors and methods below. Note that the second constructor accepts an object of type SpinnerModel as a parameter. This table also shows constructors for two classes that implement the SpinnerModel interface.

Constructor Description
JSpinner() Creates a default spinner. The default spinner lets the user
choose an integer that has an initial value of 0 and no minimum or maximum values.
JSlider(SpinnerModel model) Creates a spinner using the specified SpinnerModel object.
Method Description
void addChangeListener(ChangeListener
listener)
Adds a ChangeListener to listen for
change events.
int getValue() Gets the value.
void setToolTipText(String ext) Sets the tooltip text that’s displayed if the user rests
the mouse over the slider for a few moments.
Constructors for SpinnerModel Classes Description
SpinnerNumberModel(int init, int min, int
max, int step)
Creates a number spinner model that lets the user select
integer values ranging from min to max, with an increment of step. The initial value is set to init.
SpinnerNumberModel(double max, double
step)
Creates a number spinner model that lets the user select double
values ranging from min to max, with an increment of step. The initial value is set to init.
SpinnerListModel(Object[]values) Creates a list spinner model using the values from the
specified array.
SpinerListModel(List collection) Creates a list spinner model using the values from the
specified collection. The collection must implement the List interface.

You can create a default spinner that lets the user select integer values, like this:

JSpinner spinner = new JSpinner();

This spinner starts with a value of 0 and increases or decreases the value by 1 each time the user clicks one of the spinner’s arrows. You can retrieve the current value of the spinner at any time, like this:

int value = spinner.getValue();

For most spinners, you want to use the second constructor, which requires that you first create an object that implements the SpinnerModel interface.

There are constructors for two classes that implement Spinner. The first, SpinnerNumberModel, creates numeric spinner controls that let you control the initial value, the minimum and maximum values, and the step value that’s added or subtracted each time the user clicks one of the arrows.

Here’s how you can use the SpinnerNumberModel to create the first spinner which accepts integers from 1 to 12, starting with 1:

JSpinner hours = new JSpinner(
 new SpinnerNumberModel(1, 1, 12, 1));

Here’s the code for the second spinner, which lets the user pick numbers from 0 to 59:

JSpinner minutes = new JSpinner(
 new SpinnerNumberModel(0, 0, 59, 1));

By using the SpinnerListModel class, you can also build a spinner control that lets users select values from a list. The constructor for this class accepts either an array or an object that implements the List interface. The ArrayList class implements this interface, which means you can use SpinnerListModel to create a spinner that lets users select items from an ArrayList object.

Here’s the code for the third spinner, which lets the user specify a.m. or p.m.:

String[] ampmString = {"am", "pm"};
ampm = new JSpinner(
 new SpinnerListModel(ampmString));

In this example, the SpinnerListModel uses an array of strings with two elements: am and pm.