NumberFormatter subclasses InternationalFormatter
adding special behavior for numbers. Among the specializations are
(these are only used if the NumberFormatter does not display
invalid numbers, for example, setAllowsInvalid(false)):
DecimalFormatSymbols associated with the
DecimalFormat) in any field but the exponent
field will attempt to change the sign of the number to
positive/negative.
DecimalFormatSymbols associated with the
DecimalFormat) in the exponent field will
attempt to change the sign of the exponent to positive/negative.
If you are displaying scientific numbers, you may wish to turn on
overwrite mode, setOverwriteMode(true). For example:
DecimalFormat decimalFormat = new DecimalFormat("0.000E0");
NumberFormatter textFormatter = new NumberFormatter(decimalFormat);
textFormatter.setOverwriteMode(true);
textFormatter.setAllowsInvalid(false);
If you are going to allow the user to enter decimal
values, you should either force the DecimalFormat to contain at least
one decimal (#.0###), or allow the value to be invalid
setAllowsInvalid(true). Otherwise users may not be able to
input decimal values.
NumberFormatter provides slightly different behavior to
stringToValue than that of its superclass. If you have
specified a Class for values, setValueClass, that is one of
of Integer, Long, Float,
Double, Byte or Short and
the Format's parseObject returns an instance of
Number, the corresponding instance of the value class
will be created using the constructor appropriate for the primitive
type the value class represents. For example:
setValueClass(Integer.class) will cause the resulting
value to be created via
Integer.valueOf(((Number)formatter.parseObject(string)).intValue()).
This is typically useful if you
wish to set a min/max value as the various Number
implementations are generally not comparable to each other. This is also
useful if for some reason you need a specific Number
implementation for your values.
Warning
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeans
has been added to the java.beans package.
Please see java..
| Modifier and Type | Field and Description |
|---|---|
| private String | specialChars
The special characters from the Format instance. |
| Access | Constructor and Description |
|---|---|
| public | NumberFormatter()
Creates a |
| public | NumberFormatter(NumberFormat
Format used to dictate legal values format)Creates a NumberFormatter with the specified Format instance. |
| Modifier and Type | Method and Description |
|---|---|
| private Object | convertValueToValueClass(Object value, Class<?> valueClass)
Converts the passed in value to the passed in class. |
| private DecimalFormatSymbols | |
| private char | |
| private NumberFormat. | getFieldFrom(int index, int direction)
Returns the first |
| private char | |
| private char | |
| pack-priv boolean | isLegalInsertText(String text)
Overrides javax. Subclassed to return false if |
| pack-priv boolean | isLiteral(Map<?, ?> attrs)
Overrides javax. Subclassed to treat the decimal separator, grouping separator, exponent symbol, percent, permille, currency and sign as literals. |
| pack-priv boolean | isNavigatable(int index)
Overrides javax. Subclassed to make the decimal separator navigable, as well as making the character between the integer field and the next field navigable. |
| pack-priv void | replace(DocumentFilter.
Overrides javax. Overridden to toggle the value if the positive/minus sign is inserted. |
| public void | setFormat(Format
NumberFormat instance used to dictate legal values format)Overrides javax. Sets the format that dictates the legal values that can be edited and displayed. |
| pack-priv Object | stringToValue(String text, Format f)
Overrides javax. Invokes |
| private Object | toggleExponentSign(int offset, char aChar)
Invoked to toggle the sign of the exponent (for scientific numbers). |
| private Object | |
| private boolean | toggleSignIfNecessary(DocumentFilter.
Will change the sign of the integer or exponent field if
|
| specialChars | back to summary |
|---|---|
| private String specialChars The special characters from the Format instance. | |
| NumberFormatter | back to summary |
|---|---|
| public NumberFormatter() Creates a | |
| NumberFormatter | back to summary |
|---|---|
| public NumberFormatter(NumberFormat format) Creates a NumberFormatter with the specified Format instance.
| |
| convertValueToValueClass | back to summary |
|---|---|
| private Object convertValueToValueClass(Object value, Class<?> valueClass) Converts the passed in value to the passed in class. This only
works if | |
| getDecimalFormatSymbols | back to summary |
|---|---|
| private DecimalFormatSymbols getDecimalFormatSymbols() Returns the DecimalFormatSymbols from the Format instance. | |
| getDecimalSeparator | back to summary |
|---|---|
| private char getDecimalSeparator() Returns the character that is used to toggle to negative values. | |
| getFieldFrom | back to summary |
|---|---|
| private NumberFormat. Returns the first | |
| getMinusSign | back to summary |
|---|---|
| private char getMinusSign() Returns the character that is used to toggle to negative values. | |
| getPositiveSign | back to summary |
|---|---|
| private char getPositiveSign() Returns the character that is used to toggle to positive values. | |
| isLegalInsertText | back to summary |
|---|---|
| pack-priv boolean isLegalInsertText(String text) Overrides javax. Subclassed to return false if | |
| isLiteral | back to summary |
|---|---|
| pack-priv boolean isLiteral(Map<?, ?> attrs) Overrides javax. Subclassed to treat the decimal separator, grouping separator, exponent symbol, percent, permille, currency and sign as literals. | |
| isNavigatable | back to summary |
|---|---|
| pack-priv boolean isNavigatable(int index) Overrides javax. Subclassed to make the decimal separator navigable, as well as making the character between the integer field and the next field navigable. | |
| replace | back to summary |
|---|---|
| pack-priv void replace(DocumentFilter. Overrides javax. Overridden to toggle the value if the positive/minus sign is inserted. | |
| setFormat | back to summary |
|---|---|
| public void setFormat(Format format) Overrides javax. Sets the format that dictates the legal values that can be edited and displayed.
If you have used the nullary constructor the value of this property
will be determined for the current locale by way of the
| |
| stringToValue | back to summary |
|---|---|
| pack-priv Object stringToValue(String text, Format f) throws ParseException Overrides javax. Invokes | |
| toggleExponentSign | back to summary |
|---|---|
| private Object toggleExponentSign(int offset, char aChar) throws BadLocationException, ParseException Invoked to toggle the sign of the exponent (for scientific numbers). | |
| toggleSign | back to summary |
|---|---|
| private Object toggleSign(boolean positive) throws ParseException Invoked to toggle the sign. For this to work the value class must have a single arg constructor that takes a String. | |
| toggleSignIfNecessary | back to summary |
|---|---|
| private boolean toggleSignIfNecessary(DocumentFilter. Will change the sign of the integer or exponent field if
| |