APIdia Launch Partner
Top Description Fields Constructors Methods
javax.swing.text

public Class NumberFormatter

extends InternationalFormatter
Class Inheritance
Known Direct Subclasses
javax.swing.JSpinner.NumberEditorFormatter
Annotations
@SuppressWarnings:serial
Imports
java.lang.reflect.Constructor, java.text.AttributedCharacterIterator, .DecimalFormat, .DecimalFormatSymbols, .Format, .NumberFormat, .ParseException, java.util.Map, sun.swing.SwingUtilities2

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)):

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.beans.XMLEncoder.

Since
1.4

Field Summary

Modifier and TypeField and Description
private String
specialChars

The special characters from the Format instance.

Constructor Summary

AccessConstructor and Description
public
NumberFormatter()

Creates a NumberFormatter with the default NumberFormat instance obtained from NumberFormat.getNumberInstance().

public
NumberFormatter(NumberFormat
Format used to dictate legal values
format
)

Creates a NumberFormatter with the specified Format instance.

Method Summary

Modifier and TypeMethod and Description
private Object
convertValueToValueClass(Object value, Class<?> valueClass)

Converts the passed in value to the passed in class.

private DecimalFormatSymbols
getDecimalFormatSymbols()

Returns the DecimalFormatSymbols from the Format instance.

private char
getDecimalSeparator()

Returns the character that is used to toggle to negative values.

private NumberFormat.Field
getFieldFrom(int index, int direction)

Returns the first NumberFormat.Field starting index incrementing by direction.

private char
getMinusSign()

Returns the character that is used to toggle to negative values.

private char
getPositiveSign()

Returns the character that is used to toggle to positive values.

pack-priv boolean
isLegalInsertText(String text)

Overrides javax.swing.text.DefaultFormatter.isLegalInsertText.

Subclassed to return false if text contains in an invalid character to insert, that is, it is not a digit (Character.isDigit()) and not one of the characters defined by the DecimalFormatSymbols.

pack-priv boolean
isLiteral(Map<?, ?> attrs)

Overrides javax.swing.text.InternationalFormatter.isLiteral.

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.swing.text.InternationalFormatter.isNavigatable.

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.FilterBypass fb, int offset, int length, String string, AttributeSet attr)

Overrides javax.swing.text.InternationalFormatter.replace.

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.swing.text.InternationalFormatter.setFormat.

Sets the format that dictates the legal values that can be edited and displayed.

pack-priv Object
stringToValue(String text, Format f)

Overrides javax.swing.text.InternationalFormatter.stringToValue.

Invokes parseObject on f, returning its value.

private Object
toggleExponentSign(int offset, char aChar)

Invoked to toggle the sign of the exponent (for scientific numbers).

private Object
toggleSign(boolean positive)

Invoked to toggle the sign.

private boolean
toggleSignIfNecessary(DocumentFilter.FilterBypass fb, int offset, char aChar)

Will change the sign of the integer or exponent field if aChar is the positive or minus sign.

Inherited from javax.swing.text.InternationalFormatter:
adjustValuecanIncrementcanReplaceclonegetActionsgetAdjustFieldgetAttributesgetAttributeStartgetBufferedChargetFieldsgetFormatgetIteratorgetLiteralgetLiteralCountTogetMaximumgetMinimumgetReplaceHoldergetSupportsIncrementinstallisLiteralisValidMaskisValidValuereplaceresetValueselectFieldsetMaximumsetMinimumstringToValueupdateMaskupdateMaskIfNecessaryupdateValuevalueToString

Field Detail

specialCharsback to summary
private String specialChars

The special characters from the Format instance.

Constructor Detail

NumberFormatterback to summary
public NumberFormatter()

Creates a NumberFormatter with the default NumberFormat instance obtained from NumberFormat.getNumberInstance().

NumberFormatterback to summary
public NumberFormatter(NumberFormat format)

Creates a NumberFormatter with the specified Format instance.

Parameters
format:NumberFormat

Format used to dictate legal values

Method Detail

convertValueToValueClassback to summary
private Object convertValueToValueClass(Object value, Class<?> valueClass)

Converts the passed in value to the passed in class. This only works if valueClass is one of Integer, Long, Float, Double, Byte or Short and value is an instanceof Number.

getDecimalFormatSymbolsback to summary
private DecimalFormatSymbols getDecimalFormatSymbols()

Returns the DecimalFormatSymbols from the Format instance.

getDecimalSeparatorback to summary
private char getDecimalSeparator()

Returns the character that is used to toggle to negative values.

getFieldFromback to summary
private NumberFormat.Field getFieldFrom(int index, int direction)

Returns the first NumberFormat.Field starting index incrementing by direction.

getMinusSignback to summary
private char getMinusSign()

Returns the character that is used to toggle to negative values.

getPositiveSignback to summary
private char getPositiveSign()

Returns the character that is used to toggle to positive values.

isLegalInsertTextback to summary
pack-priv boolean isLegalInsertText(String text)

Overrides javax.swing.text.DefaultFormatter.isLegalInsertText.

Subclassed to return false if text contains in an invalid character to insert, that is, it is not a digit (Character.isDigit()) and not one of the characters defined by the DecimalFormatSymbols.

isLiteralback to summary
pack-priv boolean isLiteral(Map<?, ?> attrs)

Overrides javax.swing.text.InternationalFormatter.isLiteral.

Subclassed to treat the decimal separator, grouping separator, exponent symbol, percent, permille, currency and sign as literals.

isNavigatableback to summary
pack-priv boolean isNavigatable(int index)

Overrides javax.swing.text.InternationalFormatter.isNavigatable.

Subclassed to make the decimal separator navigable, as well as making the character between the integer field and the next field navigable.

replaceback to summary
pack-priv void replace(DocumentFilter.FilterBypass fb, int offset, int length, String string, AttributeSet attr) throws BadLocationException

Overrides javax.swing.text.InternationalFormatter.replace.

Overridden to toggle the value if the positive/minus sign is inserted.

setFormatback to summary
public void setFormat(Format format)

Overrides javax.swing.text.InternationalFormatter.setFormat.

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 NumberFormat.getNumberInstance() method.

Parameters
format:Format

NumberFormat instance used to dictate legal values

stringToValueback to summary
pack-priv Object stringToValue(String text, Format f) throws ParseException

Overrides javax.swing.text.InternationalFormatter.stringToValue.

Invokes parseObject on f, returning its value.

toggleExponentSignback to summary
private Object toggleExponentSign(int offset, char aChar) throws BadLocationException, ParseException

Invoked to toggle the sign of the exponent (for scientific numbers).

toggleSignback 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.

toggleSignIfNecessaryback to summary
private boolean toggleSignIfNecessary(DocumentFilter.FilterBypass fb, int offset, char aChar) throws BadLocationException

Will change the sign of the integer or exponent field if aChar is the positive or minus sign. Returns true if a sign change was attempted.