Java Reference
In-Depth Information
Table 14-8.
JSpinner.ListEditor Properties
Property Name
Data Type
Access
model
SpinnerListModel
Read-only
JSpinner.NumberEditor Class
The
NumberEditor
works in a manner similar to the
DateEditor
, allowing you to enter strings to
customize the display format. Instead of working with the
SimpleDateFormat
, the
NumberEditor
is associated with the
DecimalFormat
class of the
java.text
package. Just like
DateEditor
, it has
two constructors:
public JSpinner.NumberEditor(JSpinner spinner)
SpinnerModel model = new SpinnerNumberModel(50, 0, 100, .25);
JSpinner spinner = new JSpinner(model);
JComponent editor = JSpinner.NumberEditor(spinner);
spinner.setEditor(editor);
public JSpinner.NumberEditor(JSpinner spinner, String decimalFormatPattern)
SpinnerModel model = new SpinnerNumberModel(50, 0, 100, .25);
JSpinner spinner = new JSpinner(model);
JComponent editor = JSpinner.NumberEditor(spinner, "#,##0.###");
spinner.setEditor(editor);
The second constructor usage shows the default formatting string. It will try to use commas
if the number is large enough, and it will not show decimals if the value is a whole number.
As shown in Table 14-9, this editor has two properties.
Table 14-9.
JSpinner.NumberEditor Properties
Property Name
Data Type
Access
format
DecimalFormat
Read-only
model
SpinnerNumberModel
Read-only
Summary
In this chapter, you learned about Swing's
JSpinner
component. When your set of choices is
limited to a fixed set or a range of values, a
JSpinner
allows you to select a value by spinning
through the different choices. You learned how that set of choices could be provided: from a set
of dates with the
SpinnerDateModel
and
DateEditor
, with the
SpinnerListModel
and
ListEditor
,
or via the
SpinnerNumberModel
and
NumberEditor
.
Chapter 15 moves beyond selection from a range of values and on to where a user actually
types in the whole content in the different text components.