The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Creating a GUI with JFC/Swing
Lesson: Using Swing Components

How to Use Formatted Text Fields

In release 1.4, JFormattedTextField was introduced. Formatted text fields provide a way for developers to specify the legal set of characters that can be entered into a text component.

Version Note: Before 1.4, text field formatting required more effort. See Creating a Validated Text Field(in the Creating a GUI with JFC/Swing trail) for a full example.

[PENDING: One day, this page will discuss formatted text fields. For now, it only has links to API. You can find more information about using the API in Formatted Text Fields(outside of the tutorial), which is part of the J2SE v 1.4 release notes.]

The following tables list some of the commonly used API for using formatted text fields.

Classes Related to Formatted Text Fields
Class or Interface Purpose
JFormattedTextField Subclass of JTextField that supports formatting arbitrary values.
JFormattedTextField.AbstractFormatter The superclass of all formatters for JFormattedTextField. A formatter enforces editing policies and navigation policies, handles string-to-object conversions, and manipulates the JFormattedTextField as necessary to enforce the desired policy.
JFormattedTextField.AbstractFormatterFactory The superclass of all formatter factories. Each JFormattedTextField uses a formatter factory to obtain the formatter that best corresponds to the text field's state.
DefaultFormatterFactory The formatter factory normally used. Dishes out formatters based on details such as the passed-in parameters and focus state.
DefaultFormatter Subclass of JFormattedTextField.AbstractFormatter that formats arbitrary objects using the toString method.
MaskFormatter Subclass of DefaultFormatter that formats and edits strings using a specified character mask. (For example, 7-digit phone numbers can be specified using "###-####".)
InternationalFormatter Subclass of DefaultFormatter that uses an instance of java.text.Format to handle conversion to and from a String.
NumberFormatter Subclass of InternationalFormatter that supports number formats using an instance of NumberFormat.
DateFormatter Subclass of InternationalFormatter that supports date formats using an instance of DateFormat.

JFormattedTextField Methods
Method or Constructor Purpose
JFormattedTextField()
JFormattedTextField(Object)
JFormattedTextField(Format)
JFormattedTextField(JFormattedTextField.AbstractFormatter)
JFormattedTextField(JFormattedTextField.AbstractFormatterFactory)
JFormattedTextField(JFormattedTextField.AbstractFormatterFactory, Object)
Create a new formatted text field.
void setValue(Object)
Object getValue()
Set or get the value of the formatted text field. You must cast the return type based on how the JFormattedTextField has been configured.

DefaultFormatter Options
Method Purpose
void setCommitsOnValidEdit(boolean)
boolean getCommitsOnValidEdit()
Set or get when edits are pushed back to the JFormattedTextField If true, commitEdit is invoked after every valid edit.
void setOverwriteMode(boolean)
boolean getOverwriteMode()
Set or get the the behavior when inserting characters. If true (the default), new characters overwrite existing characters in the model as they are inserted.
void setAllowsInvalid(boolean)
boolean getAllowsInvalid()
Set or get whether the value being edited is allowed to be invalid for a length of time. It is often convenient to allow the user to enter invalid values until a commit is attempted. If true, commitEdit is invoked after every valid edit.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.