Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

The Definitive Guide to Java Swing: Spinner Model Controls

Learn how to work with JSpinner

  • Print
  • Feedback

The JSpinner works like a cross between a JListor 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 JFormattedTextFieldfor entry and a couple of arrows on the side to navigate through the different values available for the text field.

Figure 1 shows what the spinner looks like for several different types of input. At the top of Figure 1 is a JSpinner with the days of the week in French provided to a SpinnerListModel. In the middle, you have a JSpinnerfor a date via the SpinnerDateModelclass. On the bottom is the JSpinnerusage with the SpinnerNumberModel. Each of these three works in its own mysterious way, as you'll learn later in this article.

Figure 1. JSpinner examples

Many classes are involved when creating and manipulating JSpinnercomponents, foremost, the JSpinnerclass 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.DefaultEditorimplementations, 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.

Creating JSpinner components

The JSpinner class includes two constructors for initializing the component:

                                           public JSpinner()JSpinner spinner = new JSpinner();
                        public JSpinner(SpinnerModel model)SpinnerModel model = new SpinnerListModel(args);
JSpinner spinner = new JSpinner(model);
               


You can start with no data model and associate it later with the tracking method of JSpinner. Alternatively, you can start up the component with a full model, in an implementation of the SpinnerModel interface, of which three concrete subclasses are available: SpinnerDateModel, SpinnerListModel, and SpinnerNumberModel, along with their abstract parent class AbstractSpinnerModel. If you don't specify a model, the SpinnerNumberModel is used. While the renderer and editor for the component is a JFormattedTextField, the editing is basically done through a series of inner classes of JSpinner: DateEditor, ListEditor, and NumberFormat, with its support class in its parent DefaultEditor.

JSpinner properties

In addition to creating the JSpinner object, you can certainly reconfigure it through one of the nine properties listed in Table 1.

Table 1. JSpinner properties

Property name Data type Access
accessibleContext AccessibleContext Read-only
changeListeners ChangeListener[ ] Read-only
editor JComponent Read-write bound
model SpinnerModel Read-write bound
nextValue Object Read-only
previousValue Object Read-only
UI SpinnerUI Read-write
UIClassID String Read-only
value Object Read-write


The value property allows you to change the current setting for the component. The nextValue and previousValueproperties allow you to peek at entries of the model in the different directions without changing the selection within the application itself.

  • Print
  • Feedback

Resources