Java Reference
In-Depth Information
Using a Spinner
We could use another list for this, but to broaden our horizons we will use another Swing component, a
JSpinner object. The JSpinner class is defined in the javax.Swing package. A JSpinner object
displays a sequence of numbers or objects so the user can select any one from the set. The spinner
displays up and down arrows at the side of the spinner for stepping through the list. You can also use
the keyboard up/down arrow keys for this.
The sequence of choices in a spinner is managed by a SpinnerModel object. There are three concrete
spinner model classes, depending on what sort of items you are choosing from:
Class
Description
SpinnerNumberModel
A model for a sequence of numbers. Numbers are stored internally and
returned as type Number , which is the superclass of the classes
encapsulating the primitive numerical types - Integer , Long ,
Double etc. Number is also the superclass of other classes such as
BigDecimal , but only the classes corresponding to the primitive types
are supported.
SpinnerListModel
A model for a sequence defined by an array or a List object. You
could use this to use a sequence of strings as the choices in the spinner.
SpinnerDateModel
A model for a sequence of dates specified as Date objects.
We won't be able to go through all the detail on these, so let's just take a JSpinner object using a
SpinnerNumberModel to contain the sequence as that fits with selecting a font size.
We will be using a fixed range of point sizes to choose from, so let's add some constants to our
Constants interface to define this:
int pointSizeMin = 8; // Minimum font point size
int pointSizeMax = 24; // Maximum font point size
int pointSizeStep = 2; // Point size step
Thus our smallest point size is eight, the largest is 24, and the step from 8 onwards is 2.
We create a JSpinner object by passing a SpinnerModel reference to it. For instance:
JSpinner spinner = new JSpinner(spinnerModel);
In our case the spinner model will be of type SpinnerNumberModel , and the constructor we will use
expects four arguments, a current value that will be the one displayed initially, a minimum value, a
maximum, and the step size. Here's how we can create that for our dialog:
public FontDialog(SketchFrame window) {
// Initialization as before...
// Button panel code as before...
// Set up the data input panel to hold all input components as before...
// Add the font choice prompt label as before...
Search WWH ::




Custom Search