Channels ▼

JVM Languages

Java: Better Interfaces via JFormattedTextField


By default, when the focus is lost on a JFormattedTextField the rules for COMMIT_OR_REVERT are followed. That is, if the text typed in is legal, the value is updated and the new value is displayed using the display formatter; if the text is not legal, then the text being edited is discarded and the old value is displayed. A JFormattedTextField can also be set to use the rules for COMMIT, under those rules when an invalid string is entered, it is left displayed, but the value is not changed. When this happens the text shown on the screen does not match the value stored in the JFormattedTextField. So it seems the choice is to lose the edit or have a mismatch in values. Neither of these options is desirable. To correct this, all you need to do is to attach an InputVerifier to the JFormattedTextField. An InputVerifier provides the capability to force the control to retain the focus if it has an invalid value. Further, the formatters attached to a JFormattedTextField usually only check the format of the data; an InputVerifier can perform range checks and other validity checks as well.

An InputVerifier is a class that inherits the InputVerifier class. These classes will contain a constructor and must define the body of an abstract method named "verify". This method is called whenever the focus is about to be lost on a control. If verify returns true, this means all is well and the focus can be moved. If verify returns false, then the focus remains on the control. The verify method accepts a JComponent as input (the JFormattedTextField) and then can do whatever manipulations it needs before returning true or false. The example below is a reusable InputVerifier to handle double values.

The data fields define the minimum and maximum permissible values. The constructor and the included mutator methods can set these. Two Color values are also present, one for invalid values, the other for valid values. The verify method first retrieves the text from the JComponent and tries to parse it to a double. If an exception is thrown, then the foreground color is set to INVALID_COLOR and false is returned. If an exception is not thrown, then the value is tested to see if is in a valid range, if it is, the foreground color is set to the VALID_COLOR and true is returned. If the value is out of range then the foreground color is set to INVALID_COLOR and false is returned. (Also see which you can download here.)

To use this InputVerifier, first create an instance of the verifier, then attach it to the JFormattedTextField using the setInputVerifier method.

  JFormattedTextField salaryField = new JFormattedTextField();
 DoubleVerifier salaryVerifier = new DoubleVerifier(0,1000000);    
 salaryField.setInputVerifier(salaryVerifier);  // attach the verifier

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.