The NumberFormatter Class
NumberFormatter is a subclass of InternationalFormatter that uses an object of
type java.text.NumberFormat as its
format. JFormattedTextField instantiates a NumberFormatter for itself if you pass a
subclass of Number or a java.text.NumberFormat into its constructor.
With a subclass of Number, the format
is localized to the current locale. Again, this is usually what you
want, but if you want your program to use the same number format no
matter where in the world it is run, construct your JFormattedTextField with a specific java.text.NumberFormat.
Properties
NumberFormatter does not
define any properties beyond those it inherits (see Table 20-6). The minimum and maximum properties can be handy if you want
the field to be restricted to a specific range.
Constructors
- public NumberFormatter( )
Create a new
NumberFormatterand calljava.text.NumberFormat.getNumberInstance( )to set theformat, which is a general-purposeformatfor the current locale.- public NumberFormatter(java.text.NumberFormat format)
Create a new
NumberFormatterwith the specifiedformat.
Public Method
- public Object stringToValue(String text) throws java.text.ParseException
NumberFormatterdoes not technically override this method, but (through private methods) does alter how it works slightly. This method should succeed ifvalueClasshas been set toInteger,Long,Short,Byte,Float, orDouble, even though those classes don’t have constructors that take a singleStringargument.
The JFormattedTextField.AbstractFormatterFactory ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access