Class NumberFormatter
- All Implemented Interfaces:
Serializable, Cloneable
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)
):
- Pressing +/- (- is determined from the
DecimalFormatSymbols
associated with theDecimalFormat
) in any field but the exponent field will attempt to change the sign of the number to positive/negative. - Pressing +/- (- is determined from the
DecimalFormatSymbols
associated with theDecimalFormat
) 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, DefaultFormatter.setValueClass(Class)
, 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 XMLEncoder
.
- Since:
- 1.4
-
Constructor Summary
ConstructorsConstructorDescriptionCreates aNumberFormatter
with the defaultNumberFormat
instance obtained fromNumberFormat.getNumberInstance()
.NumberFormatter
(NumberFormat format) Creates a NumberFormatter with the specified Format instance. -
Method Summary
Methods declared in class InternationalFormatter
clone, getActions, getFields, getFormat, getMaximum, getMinimum, install, setMaximum, setMinimum, stringToValue, valueToString
Modifier and TypeMethodDescriptionclone()
Creates a copy of the DefaultFormatter.protected Action[]
IfgetSupportsIncrement
returns true, this returns two Actions suitable for incrementing/decrementing the value.getFields
(int offset) Returns theFormat.Field
constants associated with the text atoffset
.Returns the format that dictates the legal values that can be edited and displayed.Comparable
<?> Returns the maximum permissible value.Comparable
<?> Returns the minimum permissible value.void
Installs theDefaultFormatter
onto a particularJFormattedTextField
.void
setMaximum
(Comparable<?> max) Sets the maximum permissible value.void
setMinimum
(Comparable<?> minimum) Sets the minimum permissible value.stringToValue
(String text) Returns theObject
representation of theString
text
.valueToString
(Object value) Returns a String representation of the Objectvalue
.Methods declared in class DefaultFormatter
getAllowsInvalid, getCommitsOnValidEdit, getDocumentFilter, getNavigationFilter, getOverwriteMode, getValueClass, setAllowsInvalid, setCommitsOnValidEdit, setOverwriteMode, setValueClass
Modifier and TypeMethodDescriptionboolean
Returns whether or not the value being edited is allowed to be invalid for a length of time.boolean
Returns when edits are published back to theJFormattedTextField
.protected DocumentFilter
Returns theDocumentFilter
used to restrict the characters that can be input into theJFormattedTextField
.protected NavigationFilter
Returns theNavigationFilter
used to restrict where the cursor can be placed.boolean
Returns the behavior when inserting characters.Class
<?> Returns that class that is used to create new Objects.void
setAllowsInvalid
(boolean allowsInvalid) Sets whether or not the value being edited is allowed to be invalid for a length of time (that is,stringToValue
throws aParseException
).void
setCommitsOnValidEdit
(boolean commit) Sets when edits are published back to theJFormattedTextField
.void
setOverwriteMode
(boolean overwriteMode) Configures the behavior when inserting characters.void
setValueClass
(Class<?> valueClass) Sets that class that is used to create new Objects.Methods declared in class JFormattedTextField.AbstractFormatter
getFormattedTextField, invalidEdit, setEditValid, uninstall
Modifier and TypeMethodDescriptionprotected JFormattedTextField
Returns the currentJFormattedTextField
theAbstractFormatter
is installed on.protected void
This should be invoked when the user types an invalid character.protected void
setEditValid
(boolean valid) Invoke this to update theeditValid
property of theJFormattedTextField
.void
Uninstalls any state theAbstractFormatter
may have installed on theJFormattedTextField
.Methods declared in class Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Modifier and TypeMethodDescriptionboolean
Indicates whether some other object is "equal to" this one.protected void
finalize()
Deprecated, for removal: This API element is subject to removal in a future version.Finalization is deprecated and subject to removal in a future release.final Class
<?> getClass()
Returns the runtime class of thisObject
.int
hashCode()
Returns a hash code value for this object.final void
notify()
Wakes up a single thread that is waiting on this object's monitor.final void
Wakes up all threads that are waiting on this object's monitor.toString()
Returns a string representation of the object.final void
wait()
Causes the current thread to wait until it is awakened, typically by being notified or interrupted.final void
wait
(long timeoutMillis) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.final void
wait
(long timeoutMillis, int nanos) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
-
Constructor Details
-
NumberFormatter
public NumberFormatter()Creates aNumberFormatter
with the defaultNumberFormat
instance obtained fromNumberFormat.getNumberInstance()
. -
NumberFormatter
Creates a NumberFormatter with the specified Format instance.- Parameters:
format
- Format used to dictate legal values
-
-
Method Details
-
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.- Overrides:
setFormat
in classInternationalFormatter
- Parameters:
format
- NumberFormat instance used to dictate legal values
-