Java Reference
In-Depth Information
Table 15-7. JPasswordField UIResource Elements (Continued)
Property String
Object Type
PasswordField.margin
Insets
PasswordField.selectionBackground
Color
PasswordField.selectionForeground
Color
PasswordFieldUI
String
JFormattedTextField Class
The JFormattedTextField provides support for the input of formatted text. When the compo-
nent is created, you define a mask for the input. That mask can be in the form of one of four
styles: a java.text.Format object, an AbstractFormatter , an AbstractFormatterFactory , or an
actual value of a different type (such as 3.141592).
The system provides several abstract formatters for you to work with, depending on the
type of data you want a user to input. For instance, the NumberFormatter is available to enter
numbers, and the DateFormatter is available for entering dates. There is also a MaskFormatter for
describing input with edit strings, like "XXX-XX-XXX" for a United States social security number. If
you want different display and edit formats, you can use the AbstractFormatterFactory . You'll
learn more about formatters and formatter factories in Chapter 16.
Creating a JFormattedTextField
There are six constructors for the JFormattedTextField class:
public JFormattedTextField()
JFormattedTextField formattedField = new JFormattedTextField();
public JFormattedTextField(Format format)
DateFormat format = new SimpleDateFormat("yyyy--MMMM--dd");
JFormattedTextField formattedField = new JFormattedTextField(format);
public JFormattedTextField(JFormattedTextField.AbstractFormatter formatter)
DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
DateFormatter displayFormatter = new DateFormatter(displayFormat);
JFormattedTextField formattedField = new JFormattedTextField(displayFormatter);
public JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory)
DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
DateFormatter displayFormatter = new DateFormatter(displayFormat);
DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
DateFormatter editFormatter = new DateFormatter(editFormat);
DefaultFormatterFactory factory = new DefaultFormatterFactory(
displayFormatter, displayFormatter, editFormatter);
JFormattedTextField formattedField = new JFormattedTextField(factory);
 
Search WWH ::




Custom Search