![]() |
The Java Developers Almanac 1.4 |
|
e997. Creating a Text Field to Display and Edit a Phone NumberThis example uses aJFormattedTextField to allow the display and
editing of certain fixed-string patterns. By default, when the
component loses the focus and the modified value is valid, the
modified value is saved. Otherwise, if the modified value is not
valid, the modified value is discarded and the old value is displayed.
The pattern is specified using one of the following characters:
MaskFormatter fmt = null;
// A phone number
try {
fmt = new MaskFormatter("###-###-####");
} catch (java.text.ParseException e) {
}
JFormattedTextField tft1 = new JFormattedTextField(fmt);
// A social security number
try {
fmt = new MaskFormatter("###-##-####");
} catch (java.text.ParseException e) {
}
JFormattedTextField tft2 = new JFormattedTextField(fmt);
The spot where a character or digit is expected is called a
placeholder. By default, a placeholder is represented with a space
character. The space is automatically replaced as the user fills in
the field. This example demonstrates how to use an asterisk as the
placeholder character.
// A social security number
fmt.setPlaceholderCharacter('*');
JFormattedTextField tft3 = new JFormattedTextField(fmt);
e996. Creating a Text Field to Display and Edit a Date
© 2002 Addison-Wesley. |