Java Reference
In-Depth Information
merical types — Integer , Long , Double , and so on. Number is also the superclass of other classes
such as BigDecimal , but only the classes corresponding to the primitive types are supported.
SpinnerListModel is a model for a sequence defined by an array of objects of any type, or by a
java.util.List<> object. You could use this to use a sequence of strings as the choices in the
spinner.
SpinnerDateModel is a model for a sequence of dates specified as java.util.Date objects.
I'm not able to go through all the detail on these, so let's just take a JSpinner object using a SpinnerNum-
berModel class to contain the sequence, as that fits with selecting a font size.
You use a fixed range of point sizes to choose from, so let's add some constants to the SketcherCon-
stants class to define this:
public final static int POINT_SIZE_MIN = 8;
// Minimum font point size
public final static int POINT_SIZE_MAX = 24;
// Maximum font point size
public final static int POINT_SIZE_STEP = 2;
// Point size step public
Thus the smallest point size that can be chosen is 8, the largest is 24, and the step from 8 onward is 2.
You create a JSpinner object by passing a SpinnerModel reference to it. For example:
JSpinner spinner = new JSpinner(spinnerModel);
In the font dialog, the spinner model is of type SpinnerNumberModel , and the constructor you use to cre-
ate the object expects four arguments: a current value that is the one displayed initially, a minimum value, a
maximum value, and the step size. Here's how you can create that for the font dialog:
public FontDialog(SketcherFrame 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...
// Set up font list choice component as before...
// Panel to display font sample as before...
// Create a split pane with font choice at the top as before...
// Set up the size choice using a spinner
JPanel sizePane = new JPanel(true);
// Pane for size choices
label = new JLabel("Choose point size: ");
// Prompt for point size
sizePane.add(label);
// Add the prompt
chooseSize = new JSpinner(new SpinnerNumberModel(font.getSize(),
POINT_SIZE_MIN, POINT_SIZE_MAX, POINT_SIZE_STEP));
chooseSize.setValue(font.getSize());
// Set current font size
chooseSize.addChangeListener(this);
sizePane.add(chooseSize);
// Add spinner to pane
gbLayout.setConstraints(sizePane, constraints); // Set pane constraints
dataPane.add(sizePane); // Add the pane
// Plus the code for the rest of the constructor...
Search WWH ::




Custom Search