Java Reference
In-Depth Information
Figure 16-8. Formatted masked input in JFormattedTextFields
The MaskFormatter does provide some customization options. By default, the formatter is
in overwrite mode, so as you type, the entered digits will replace each number and space in the
input field. Set the overwriteMode property to false to disable this. Typically, this isn't necessary,
though for entering in long dates, it can be helpful.
If you wish to have a different character as the placeholder character, before a position is
filled in the mask, set the placeholderCharacter property of the MaskFormatter . To demonstrate,
add the following line to the phone number formatter in Listing 16-9:
formatter.setPlaceholder('*');
You'll see the results in the bottom text field shown in Figure 16-9.
Figure 16-9. Formatted masked input with a placeholder
Another useful MaskFormatter property is validCharacters , for restricting which alpha-
numeric characters are valid for an input field.
DefaultFormatterFactory Class
The DefaultFormatterFactory class found in the javax.swing.text package offers a way to
have different formatters for displaying values, editing values, and a special case of a null
value. It offers five constructors, starting with no parameters and then adding an additional
AbstractFormatter parameter for each constructor.
public DefaultFormatterFactory()
DefaultFormatterFactory factory = new DefaultFormatterFactory()
public DefaultFormatterFactory(JFormattedTextField.AbstractFormatter defaultFormat)
DateFormat defaultFormat = new SimpleDateFormat("yyyy--MMMM--dd");
DateFormatter defaultFormatter = new DateFormatter(displayFormat);
DefaultFormatterFactory factory = new DefaultFormatterFactory(defaultFormatter);
public DefaultFormatterFactory(JFormattedTextField.AbstractFormatter defaultFormat,
JFormattedTextField.AbstractFormatter displayFormat)
DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
DateFormatter displayFormatter = new DateFormatter(displayFormat);
DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter,
displayFormatter);
 
Search WWH ::




Custom Search