Channels ▼
RSS

Database

Java: Better Interfaces via JFormattedTextField


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


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video