MaskFormatter
The MaskFormatter is best used to format Strings that have a fixed length and set format. Examples include: dates, Social Security Numbers, phone numbers, credit-card numbers, ZIP codes, vehicle ID numbers (VINs). MaskFormatters are not suitable for Strings with variable numbers of characters, e.g. URLs and email addresses. A single instance of the MaskFormatter can be used for both the edit and display formats of an AbstractFormatterFactory. A MaskFormatter object is created using a constructor that accepts a formatting mask as its only parameter. The formatting mask specifies what type of characters can appear in each position and any literal characters. Literal characters are fixed in place and are not changeable by the users; for example, the dash and parenthesis in a phone number would be considered literal characters. Table 2 shows the different mask characters used by the MaskFormatter.
For example, the mask "###-##-####" could be used for a Social Security number. The #s allow the user to type in any number, the dashes are literals and fixed in place. Similarly for a ZIP code the mask "#####-####" could be used:
MaskFormatter zipMask = new MaskFormatter("#####-####");
MaskFormatter ssnMask = new MaskFormatter("###-##-####");
You can restrict what the user types in even more by invoking the setValidCharacters or the setInvalidCharacters method. For example, if you wanted to restrict the digits in a ZIP code to just 2s and 3s, then you could make the following call:
zipMask.setValidCharacters("23"); // only allow 2, 3 in the zip code
Alternatively, if you wanted any digit but a 2 or a 3 then you could make the following call:
zipMask.setInvalidCharacters("23"); // don't allow 2,3 in the zip code.
A final consideration when creating a MaskFormatter is setting the placeholder character. This character is inserted into the String if the current value has too few characters or when the user presses the delete or backspace keys while editing. It is common to use an underscore as the placeholder, but any character will do. To avoid confusion, select a character that is not used as a literal or valid character in the mask. To set the placeholder, call the setPlaceholderCharacter method; for example:
zipMask.setPlaceholderCharacter('_'); // use an underscore as a placeholder
The Table 3 provides a few common masks.
(You may want to consider using a fixed-width font (such as Courier) for the JFormattedTextField. This makes the overall appearance and character spacing more pleasing to the eye.
The example below creates a JFormattedTextField for entering a Date:
// create a MaskFormatter for Dates
JFormattedTextField dateField = new JFormattedTextField();
MaskFormatter dateFormatter = new MaskFormatter("##/##/#### ##:##:## UM");
dateFormatter.setValidCharacters("0123456789AP");
dateFormatter.setPlaceholderCharacter('_');
dateFormatter.setValueClass(String.class);
DefaultFormatterFactory dateFormatterFactory = new
DefaultFormatterFactory(dateFormatter);
dateField.setFormatterFactory(dateFormatterFactory);
The example below creates a JFormattedTextField for entering a Social Security number:
JFormattedTextField ssnField = new JFormattedTextField();
MaskFormatter ssnFormatter = new MaskFormatter("###-##-####");
ssnFormatter.setValueClass(String.class);
ssnFormatter.setPlaceholderCharacter('_');
DefaultFormatterFactory ssnFormatterFactory = new
DefaultFormatterFactory(ssnFormatter);
ssnField.setFormatterFactory(ssnFormatterFactory);


