NumberFormatter
A NumberFormatter can be used for any numeric field, age, income, annual rainfall, quantity, cost, etc. A NumberFormatter object is created using a constructor that accepts an object of type NumberFormat as its only parameter. Typically a DecimalFormat object (subclass of NumberFormat) is used. For example:
NumberFormatter currencyFormatter =
new NumberFormatter(new DecimalFormat("$ #,###.00"));
In the above example, a DecimalFormat object is specified that can take a numeric value and convert it to a String using the specified pattern. The "#" character is replaced with a digit (or absent if 0), the "0" is replaced with a digit. The fact that there are only two "0" to the right of the decimal point indicates only two decimal places are to be shown (rounding up/down automatically). The "," acts as a grouping separator. Since there are three "#" between the decimal point and the comma, the numbers to the left of the decimal will be grouped in sets of three. You only need to specify the position of the first grouping separator.
The example above adds the grouping separator and dollar sign. Both of these will cause parse errors when converting back to a number. Therefore, you will typically need a separate NumberFormatter for the edit and display formats. The one used for the edit format would exclude the dollar sign, grouping separator (comma), and use "#"s to the right of the decimal. For example:
NumberFormatter displayCurrencyFormatter =
new NumberFormatter(new DecimalFormat("$ #,###.00"));
NumberFormatter editCurrencyFormatter =
new NumberFormatter(new DecimalFormat("#.##"));
The getValue method of the JFormattedTextField calls the parse method within the DecimalFormat class, and may return a Long or a Double, depending on the String involved. If the String has a decimal in it, a Double is returned, if it doesn't, a Long is returned. To make the class of the return value more predictable (and avoid class cast exceptions), you should call the setValueClass method and specify whether you want a Long, Double, Float, Short, Byte, or Integer; for example:
displayCurrencyFormatter.setValueClass(Double.class); editCurrencyFormatter.setValueClass(Double.class);
The following is a complete example for creating a JFormattedTextField for entering salary:
// create the JFormattedTextField
JFormattedTextField salaryField = new JFormattedTextField();
// create the formatters, default, display, edit
NumberFormatter defaultFormatter = new NumberFormatter(new DecimalFormat("#.##"));
NumberFormatter displayFormatter =
new NumberFormatter(new DecimalFormat("$ #,###.00"));
NumberFormatter editFormatter = new NumberFormatter(new DecimalFormat("#.##"));
// set their value classes
defaultFormatter.setValueClass(Double.class);
displayFormatter.setValueClass(Double.class);
editFormatter.setValueClass(Double.class);
// create and set the DefaultFormatterFactory
DefaultFormatterFactory salaryFactory =
new DefaultFormatterFactory(defaultFormatter,displayFormatter,editFormatter);
salaryField.setFormatterFactory(salaryFactory);


