Mark Pendergast is an Associate Professor of Computer Information System at Florida Gulf Coast University.
Data quality and accuracy is key to success for every application. Databases full of invalid phone numbers, improperly formatted email addresses, or out of range numeric data are of no use to anyone. Java Swing interface programmers know all too well the work involved in error checking user inputs taken from vanilla JTextFields. Many probably have been tempted to implement interfaces using JFormattedTextFields, but have been put off by the complexity of masks, formatters, verifiers, abstract classes, and interfaces that are required. In this article, I explain the proper use of the JFormattedTextFieldand its related classes to create forms that provide both guided input for users and validated data for the application. The source code for a sample application and a library of reusable classes for common data entry items based on JFormattedTextField is available for download here.
JFormattedTextFields are alternatives to JTextFields that are well worth using. They differ from JTextFields in several ways:
- First, they maintain a current "value" associated with the field as an Object. The getValue and setValue methods are provided to access it.
- Second, they use formatter objects to control the display of the value. One formatter is used to convert the value object to a String for display when the field does not have the focus; a separate formatter is used to convert the value when it receives the focus. Therefore a field that holds currency can limit the number of decimal places shown, have a dollar sign, and have commas separating the thousands while it is not being changed, then automatically change to a form with just digits and decimals when the focus is shifted to it. A type of formatter called a MaskFormatter can provide a "type-in-the-blank" template to guide the users input. MaskFormatters are useful for data types such as phone numbers, dates, and Social Security Numbers.
- The third, and most important difference is that when combined with InputVerifiers they can handle all the parsing, range checking, and formatting required for processing GUI form interfaces. For example, Figure 1(a) shows a JFormattedTextField used to input currency with the focus, Figure 1(b) shows the same JFormattedTextField without the focus. Table 1 compares the code required to input, convert, and validate a double value using JTextField and JFormattedTextField.
Implementing a JFormattedTextField
According to the Java API:
JFormattedTextField does not do the formatting it self, rather formatting is done through an instance of JFormattedTextField.AbstractFormatter which is obtained from an instance of JFormattedTextField.AbstractFormatterFactory.
What this really means is that to make a JFormattedTextField operate you need to create an instance of an AbstractFormatterFactory. The easiest way to do this is to use a convenience class called DefaultFormatterFactory, then attach it to the JFormattedTextField. The primary purpose of the DefaultFormatterFactory is to maintain three AbstractFormatter classes for the JFormattedTextField. One abstract formatter is the default formatter, it is used if a more specific formatter cannot be found; another is the display formatter used to format the value when the JFormattedTextField does not have the focus; and the third is the edit formatter, used to format the value when the JFormattedTextField has the focus.
DefaultFormatterFactory dff = new DefaultFormatterFactory(defFormatter, displayFormatter, editFormatter); JFormattedTextField tf = new JFormattedTextField(dff);
The formatter classes are primarily responsible for providing the body for two methods, stringToValue and valueToString. stringToValue parses a String and creates an object of the proper type (Long, Double, Date, Integer, etc). valueToString takes the current value and formats it for display. AbstractFormatter classes may in turn rely on specific format classes such as DecimalFormat and SimpleDateFormat. Creating formatter classes from scratch can be a daunting task, fortunately, the standard Java API provides numerous formatter classes that can be used as is. The standard class you use is determined by the type of data you need to format. Numeric data (currencies, counts, salaries) should use the NumberFormatter class, fixed length string data should use the MaskFormatter class (social security numbers, credit card numbers, phone numbers, dates), and variable length string data will need to have a custom formatter created based on the DefautFormatter class.