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

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

20.7.2 Constructors

public NumberFormatter( )

Create a new NumberFormatter and call java.text.NumberFormat.getNumberInstance( ) to set the format, which is a general-purpose format for the current locale.

public NumberFormatter(java.text.NumberFormat format)

Create a new NumberFormatter with the specified format.

20.7.3 Public Method

public Object stringToValue(String text) throws java.text.ParseException

NumberFormatter does not technically override this method, but (through private methods) does alter how it works slightly. This method should succeed if valueClass has been set to Integer, Long, Short, Byte, Float, or Double, even though those classes don't have constructors that take a single String argument.

20.7.4 The JFormattedTextField.AbstractFormatterFactory Class

AbstractFormatterFactory is an abstract inner class of JFormattedTextField that defines the basic API (a single method) for formatter factories. There is usually no reason to extend AbstractFormatterFactory directly. Instantiate a DefaultFormatterFactory instead.

Each JFormattedTextField has its own factory. See the description of formatter factories in Section 20.1 earlier in this chapter.

20.7.4.1 Public method
public abstract JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField tf)

This method returns a formatter for tf to use. Simple factories return the same formatter instance (not even a clone) each time. Sophisticated factories might examine tf and return different formatters depending on whether it has focus, whether it is enabled, etc.



Java Swing
Graphic Java 2: Mastering the Jfc, By Geary, 3Rd Edition, Volume 2: Swing
ISBN: 0130796670
EAN: 2147483647
Year: 2001
Pages: 289
Authors: David Geary

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net