Java 2 Standard Edition (J2SE), version 1.4 adds 2 new Swing components to the available GUI element palette: JSpinner and Jformattedtextfield. In the first installment of Merlin's Magic column we introduced the JSpinner component; we're going to discuss Jformattedtextfield now.
Although the Jformattedtextfield component looks similar to JTextField, its behavior is completely different from that of JSpinner. In the simplest case, you can provide an input mask similar to "(###) ###-####" for the phone number, and it will not accept any input that does not follow that format. In more complex cases, both the display formatter and the input formatter are available. For example, when editing, the default date formatter allows scrolling between available months or days depending on the position of the cursor.
When using Jformattedtextfield, an acceptable input is either explicitly specified by the mask or specified by a value of the component. In the latter case, the component uses the factory (Factory) design pattern to find the default formatter for the specified value class. The Defaultformatterfactory component provides preinstalled dates, numbers, Java.text.Format subclasses, and all the other all-encompassing formatters.
Let's see how to use components.
To configure acceptable input
Mask input is typically configured by using an instance of the Maskformatter class. Found in the Javax.swing.text package, Maskformatter works by specifying acceptable input by using a series of characters. Each of the 8 characters in the series represents one character in the input, as shown in the following list:
# |
A number |
? |
A letter |
A |
A letter or number |
* |
Any character |
U |
A letter with lowercase characters mapped to uppercase characters equivalent to them |
L |
A letter with uppercase characters mapped to lowercase characters that are equivalent to them |
H |
A hexadecimal number (a-f, a-f, 0-9) |
' |
Used to escape another mask character |
In addition to Maskformatter, you can also specify the input format with the DateFormat and NumberFormat classes from the Java.text software package. Listing 1 shows some of the possible formats.
Listing 1. Define an input mask
// Four-digit year, followed by month name and day of month,
// each separated by two dashes (--)
DateFormat format =
new SimpleDateFormat("yyyy--MMMM--dd");
DateFormatter df = new DateFormatter(format);
// US Social Security number
MaskFormatter mf1 =
new MaskFormatter("###-##-####");
// US telephone number
MaskFormatter mf2 =
new MaskFormatter("(###) ###-####");
Once you have specified the input format, you will then pass the formatter into the Jformattedtextfield constructor as follows:
JFormattedTextField ftf1 = new
JFormattedTextField(df);
There are other configurable options that depend on the formatter you are using. For example: With Maskformatter, you can set placeholder characters with Setplaceholdercharacter (char). Also, for date fields, it helps if you initialize a field to a value so that a user knows what input format is acceptable.