Recent articles:
Popular archives:
Java: A platform for platforms
Sun's reorg may seem promising to shareholders but it's also a scramble for position. The question now is whether Sun can,
or wants to, maintain its hold on Java technology. Especially with enterprise leaders like SpringSource and RedHat investing
heavily in Java's future as a platform for platforms
Also see:
Discuss: Tim Bray on 'What Sun Should Do'
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.
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.
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
|
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.
Archived Discussions (Read only)