Java Swing Tutorial - Java Swing JSpinner

A JSpinner component combines functions from a JFormattedTextField and an editable JComboBox.

JSpinner can have a list of choices and at the same time, we can also apply a format to the displayed value.

It shows only one value at a time from the list of choices and it lets we enter a new value.

The list of choices in a JSpinner must be an ordered list.

JSpinner provides the spinning capability depending on its model.

We must provide a model for the JSpinner in its constructor unless we just need a JSpinner with a list of integers.

JSpinner supports three kinds of ordered lists of choices.

  • a list of numbers
  • a list of dates
  • a list of any other objects

It provides three classes to create a model of three different kinds of lists:

  • SpinnerNumberModel
  • SpinnerDateModel
  • SpinnerListModel

A spinner model is an instance of the SpinnerModel interface. It defines the getValue(), setValue(), getPreviousValue(), and getNextValue() methods to work with values in the JSpinner.

The SpinnerNumberModel class can spin through an ordered list of numbers. We need to specify the minimum, maximum, and current values in the list. We can also specify the step value that is used to step through the number list when we use up/down buttons of a JSpinner.

The following code creates a JSpinner with a list of numbers from 1 to 10. It lets we spin through the list in steps of 1. The current value for the field is set to 5.

int minValue  = 1;
int maxValue = 10;
int currentValue = 5;
int steps = 1;

SpinnerNumberModel  nModel = new SpinnerNumberModel(currentValue, minValue,   maxValue,  steps); 
JSpinner  numberSpinner = new JSpinner(nModel);

The SpinnerDateModel class provides a model to spin through an ordered list of dates.

We need to specify the start date, the end date, the current value, and the step.

The following code creates a JSpinner to spin through a list of dates from January 1, 2000 to December 31, 2050 in steps of one day at a time.

The current system date is set as the current value for the field.

Calendar calendar  = Calendar.getInstance();
calendar.set(2000, 1, 1);
Date  minValue  = calendar.getTime();
calendar.set(2050, 12,   31);

Date  maxValue = calendar.getTime(); 
Date  currentValue = new Date();
int steps = Calendar.DAY_OF_MONTH;  // Must be  a  Calendar field
SpinnerDateModel dModel = new SpinnerDateModel(currentValue, minValue,   maxValue,  steps);
dateSpinner = new JSpinner(dModel);

The date value in the JSpinner will be displayed in the default locale format.

The SpinnerListModel class allows us to spin through an ordered list of any objects.

We just specify an array of objects or a List object, and the JSpinner will let we spin through the list as it appears in the array or the List.

The String value from toString() method of the object is displayed in the JSpinner.

The following snippet of code creates a JSpinner to display a list of four seasons:

String[]  seasons = new String[]  {"Spring", "Summer",  "Fall", "Winter"}; 
SpinnerListModel sModel  = new SpinnerListModel(seasons);
listSpinner = new JSpinner(sModel);

A JSpinner uses an editor object to display the current value. It has the following three static inner classes to display three different kinds of ordered lists:

  • JSpinner.NumberEditor
  • JSpinner.DateEditor
  • JSpinner.ListEditor

To display a number or a date in a specific format, we need to set a new editor for the JSpinner.

The editor classes for the number and date editors let we specify the formats.

The following code sets the number format as "00".

JSpinner.NumberEditor nEditor = new JSpinner.NumberEditor(numberSpinner, "00"); numberSpinner.setEditor(nEditor);

The following code sets the date format to mm/dd/yyyy

JSpinner.DateEditor dEditor = new JSpinner.DateEditor(dateSpinner,  "mm/dd/yyyy");

We can use the getValue() method defined of a JSpinner or SpinnerModel to get the current value in the JSpinner as an Object.

SpinnerNumberModel and SpinnerDateModel define the getNumber() and getDate() method that return the Number and Date objects, respectively.