JFormattedTextField extends JTextField adding
support for formatting arbitrary values, as well as retrieving a particular
object once the user has edited the text. The following illustrates
configuring a JFormattedTextField to edit dates:
JFormattedTextField ftf = new JFormattedTextField(); ftf.setValue(new Date());
Once a JFormattedTextField has been created, you can
listen for editing changes by way of adding
a PropertyChangeListener and listening for
PropertyChangeEvents with the property name value.
JFormattedTextField allows
configuring what action should be taken when focus is lost. The possible
configurations are:
| Value | Description |
|---|---|
| JFormattedTextField.REVERT | Revert the display to match that of getValue, possibly losing
the current edit.
|
| JFormattedTextField.COMMIT | Commits the current value. If the value being edited isn't considered
a legal value by the AbstractFormatter that is, a
ParseException is thrown, then the value will not change, and
then edited value will persist.
|
| JFormattedTextField.COMMIT_OR_REVERT | Similar to COMMIT, but if the value isn't legal, behave like
REVERT.
|
| JFormattedTextField.PERSIST | Do nothing, don't obtain a new AbstractFormatter, and don't
update the value.
|
JFormattedTextField.COMMIT_OR_REVERT,
refer to setFocusLostBehavior for more information on this.
JFormattedTextField allows the focus to leave, even if
the currently edited value is invalid. To lock the focus down while the
JFormattedTextField is an invalid edit state
you can attach an InputVerifier. The following code snippet
shows a potential implementation of such an InputVerifier:
public class FormattedTextFieldVerifier extends InputVerifier {
public boolean verify(JComponent input) {
if (input instanceof JFormattedTextField) {
JFormattedTextField ftf = (JFormattedTextField)input;
AbstractFormatter formatter = ftf.getFormatter();
if (formatter != null) {
String text = ftf.getText();
try {
formatter.stringToValue(text);
return true;
} catch (ParseException pe) {
return false;
}
}
}
return true;
}
public boolean shouldYieldFocus(JComponent input) {
return verify(input);
}
}
Alternatively, you could invoke commitEdit, which would also
commit the value.
JFormattedTextField does not do the formatting itself,
rather formatting is done through an instance of
JFormattedTextField.AbstractFormatter which is obtained from
an instance of JFormattedTextField.AbstractFormatterFactory.
Instances of JFormattedTextField.AbstractFormatter are
notified when they become active by way of the
install method, at which point the
JFormattedTextField.AbstractFormatter can install whatever
it needs to, typically a DocumentFilter. Similarly, when
JFormattedTextField no longer
needs the AbstractFormatter, it will invoke
uninstall.
JFormattedTextField typically
queries the AbstractFormatterFactory for an
AbstractFormat when it gains or loses focus. Although this
can change based on the focus lost policy. If the focus lost
policy is JFormattedTextField.PERSIST
and the JFormattedTextField has been edited, the
AbstractFormatterFactory will not be queried until the
value has been committed. Similarly, if the focus lost policy is
JFormattedTextField.COMMIT and an exception
is thrown from stringToValue, the
AbstractFormatterFactory will not be queried when focus is
lost or gained.
JFormattedTextField.AbstractFormatter
is also responsible for determining when values are committed to
the JFormattedTextField. Some
JFormattedTextField.AbstractFormatters will make new values
available on every edit, and others will never commit the value. You can
force the current value to be obtained
from the current JFormattedTextField.AbstractFormatter
by way of invoking commitEdit. commitEdit will
be invoked whenever return is pressed in the
JFormattedTextField.
If an AbstractFormatterFactory has not been explicitly
set, one will be set based on the Class of the value type after
setValue has been invoked (assuming value is non-null).
For example, in the following code an appropriate
AbstractFormatterFactory and AbstractFormatter
will be created to handle formatting of numbers:
JFormattedTextField tf = new JFormattedTextField(); tf.setValue(100);
Warning
As the AbstractFormatter will
typically install a DocumentFilter on the
Document, and a NavigationFilter on the
JFormattedTextField you should not install your own. If you do,
you are likely to see odd behavior in that the editing policy of the
AbstractFormatter will not be enforced.
Warning
Swing is not thread safe. For more information see Swing's Threading Policy.
Warning
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeans
has been added to the java.beans package.
Please see java..
| Modifier and Type | Class and Description |
|---|---|
| public abstract static class | JFormattedTextField.
Instances of |
| public abstract static class | JFormattedTextField.
Instances of |
| private static class | JFormattedTextField.
CancelAction will reset the value in the JFormattedTextField when
|
| pack-priv static class | JFormattedTextField.
Used to commit the edit. |
| private class | JFormattedTextField.
Sets the dirty state as the document changes. |
| private class | JFormattedTextField.
FOCUS_LOST behavior implementation |
| Modifier and Type | Field and Description |
|---|---|
| public static final int | COMMIT
Constant identifying that when focus is lost,
|
| public static final int | COMMIT_OR_REVERT
Constant identifying that when focus is lost,
|
| private boolean | composedTextExists
Indicates the input method composed text is in the document |
| private static final Action[] | |
| private DocumentListener | documentListener
Used to set the dirty state. |
| private boolean | edited
Indicates the current value has been edited. |
| private boolean | editValid
True while the value being edited is valid. |
| private JFormattedTextField. | factory
Factory used to obtain an instance of AbstractFormatter. |
| private int | focusLostBehavior
Behavior when focus is lost. |
| private JFormattedTextField. | focusLostHandler
A handler for FOCUS_LOST event |
| private JFormattedTextField. | format
Object responsible for formatting the current value. |
| public static final int | PERSIST
Constant identifying that when focus is lost, the edited value should be left. |
| public static final int | REVERT
Constant identifying that when focus is lost, editing value should
be reverted to current value set on the
|
| private ActionMap | textFormatterActionMap
ActionMap that the TextFormatter Actions are added to. |
| private static final String | |
| private Object | value
Last valid value. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | JFormattedTextField(Object
Initial value for the JFormattedTextField value)Creates a JFormattedTextField with the specified value. |
| public | JFormattedTextField(Format
Format used to look up an AbstractFormatter format)Creates a |
| public | JFormattedTextField(JFormattedTextField.
AbstractFormatter to use for formatting. formatterCreates a |
| public | JFormattedTextField(JFormattedTextField.
AbstractFormatterFactory used for formatting. factoryCreates a |
| public | JFormattedTextField(JFormattedTextField.
AbstractFormatterFactory used for
formatting.Initial value to use currentValue)Creates a |
| Modifier and Type | Method and Description |
|---|---|
| public void | commitEdit()
Forces the current value to be taken from the
|
| public Action[] | Returns: the command listOverrides javax. Fetches the command list for the editor. |
| private JFormattedTextField. | getDefaultFormatterFactory(Object type)
Returns an AbstractFormatterFactory suitable for the passed in Object type. |
| public int | Returns: returns behavior when focus is lostReturns the behavior when focus is lost. |
| public JFormattedTextField. | Returns: AbstractFormatter used for formattingReturns the |
| public JFormattedTextField. | Returns: AbstractFormatterFactory used to determine
AbstractFormattersReturns the current |
| public String | Returns: the string "FormattedTextFieldUI"Overrides javax. Gets the class ID for a UI. |
| public Object | |
| protected void | |
| private boolean | |
| public boolean | Returns: true if the current value being edited is valid.Returns true if the current value being edited is valid. |
| protected void | processFocusEvent(FocusEvent
the e)FocusEventOverrides java. Processes any focus events, such as
|
| protected void | processInputMethodEvent(InputMethodEvent
the e)InputMethodEventOverrides javax. Processes any input method events, such as
|
| public void | setDocument(Document
the document to display/edit doc)Overrides javax. Associates the editor with a text document. |
| private void | |
| private void | setEditValid(boolean
boolean indicating if the currently edited value is
valid. isValid)Sets the validity of the edit on the receiver. |
| public void | setFocusLostBehavior(int
Identifies behavior when focus is lost behavior)Sets the behavior when focus is lost. |
| protected void | setFormatter(JFormattedTextField.
AbstractFormatter to use for formatting formatSets the current |
| private void | setFormatterActions(Action[] actions)
Resets the Actions that come from the TextFormatter to
|
| public void | setFormatterFactory(JFormattedTextField.
AbstractFormatterFactory used to lookup
instances of AbstractFormatterSets the |
| public void | |
| private void | |
| private void | writeObject(ObjectOutputStream s)
Hides javax. See |
| COMMIT | back to summary |
|---|---|
| public static final int COMMIT Constant identifying that when focus is lost,
| |
| COMMIT_OR_REVERT | back to summary |
|---|---|
| public static final int COMMIT_OR_REVERT Constant identifying that when focus is lost,
| |
| composedTextExists | back to summary |
|---|---|
| private boolean composedTextExists Indicates the input method composed text is in the document | |
| defaultActions | back to summary |
|---|---|
| private static final Action[] defaultActions | |
| documentListener | back to summary |
|---|---|
| private DocumentListener documentListener Used to set the dirty state. | |
| edited | back to summary |
|---|---|
| private boolean edited Indicates the current value has been edited. | |
| editValid | back to summary |
|---|---|
| private boolean editValid True while the value being edited is valid. | |
| factory | back to summary |
|---|---|
| private JFormattedTextField. Factory used to obtain an instance of AbstractFormatter. | |
| focusLostBehavior | back to summary |
|---|---|
| private int focusLostBehavior Behavior when focus is lost. | |
| focusLostHandler | back to summary |
|---|---|
| private JFormattedTextField. A handler for FOCUS_LOST event | |
| format | back to summary |
|---|---|
| private JFormattedTextField. Object responsible for formatting the current value. | |
| PERSIST | back to summary |
|---|---|
| public static final int PERSIST Constant identifying that when focus is lost, the edited value should be left.
| |
| REVERT | back to summary |
|---|---|
| public static final int REVERT Constant identifying that when focus is lost, editing value should
be reverted to current value set on the
| |
| textFormatterActionMap | back to summary |
|---|---|
| private ActionMap textFormatterActionMap ActionMap that the TextFormatter Actions are added to. | |
| uiClassID | back to summary |
|---|---|
| private static final String uiClassID | |
| value | back to summary |
|---|---|
| private Object value Last valid value. | |
| JFormattedTextField | back to summary |
|---|---|
| public JFormattedTextField() Creates a | |
| JFormattedTextField | back to summary |
|---|---|
| public JFormattedTextField(Object value) Creates a JFormattedTextField with the specified value. This will
create an
| |
| JFormattedTextField | back to summary |
|---|---|
| public JFormattedTextField(Format format) Creates a
| |
| JFormattedTextField | back to summary |
|---|---|
| public JFormattedTextField(JFormattedTextField. Creates a
| |
| JFormattedTextField | back to summary |
|---|---|
| public JFormattedTextField(JFormattedTextField. Creates a
| |
| JFormattedTextField | back to summary |
|---|---|
| public JFormattedTextField(JFormattedTextField. Creates a
| |
| commitEdit | back to summary |
|---|---|
| public void commitEdit() throws ParseException Forces the current value to be taken from the
| |
| getActions | back to summary |
|---|---|
| public Action[] getActions() Overrides javax. Fetches the command list for the editor. This is the list of commands supported by the plugged-in UI augmented by the collection of commands that the editor itself supports. These are useful for binding to events, such as in a keymap.
| |
| getDefaultFormatterFactory | back to summary |
|---|---|
| private JFormattedTextField. Returns an AbstractFormatterFactory suitable for the passed in Object type. | |
| getFocusLostBehavior | back to summary |
|---|---|
| public int getFocusLostBehavior() Returns the behavior when focus is lost. This will be one of
| |
| getFormatter | back to summary |
|---|---|
| public JFormattedTextField. Returns the
| |
| getFormatterFactory | back to summary |
|---|---|
| public JFormattedTextField. Returns the current
| |
| getUIClassID | back to summary |
|---|---|
| public String getUIClassID() Overrides javax. Gets the class ID for a UI.
| |
| getValue | back to summary |
|---|---|
| public Object getValue() Returns the last valid value. Based on the editing policy of
the
| |
| invalidEdit | back to summary |
|---|---|
| protected void invalidEdit() Invoked when the user inputs an invalid value. This gives the component a chance to provide feedback. The default implementation beeps. | |
| isEdited | back to summary |
|---|---|
| private boolean isEdited() Returns true if the receiver has been edited. | |
| isEditValid | back to summary |
|---|---|
| public boolean isEditValid() Returns true if the current value being edited is valid. The value of
this is managed by the current
| |
| processFocusEvent | back to summary |
|---|---|
| protected void processFocusEvent(FocusEvent e) Overrides java. Processes any focus events, such as
| |
| processInputMethodEvent | back to summary |
|---|---|
| protected void processInputMethodEvent(InputMethodEvent e) Overrides javax. Processes any input method events, such as
| |
| setDocument | back to summary |
|---|---|
| public void setDocument(Document doc) Overrides javax. Associates the editor with a text document. The currently registered factory is used to build a view for the document, which gets displayed by the editor after revalidation. A PropertyChange event ("document") is propagated to each listener.
| |
| setEdited | back to summary |
|---|---|
| private void setEdited(boolean edited) Sets the edited state of the receiver. | |
| setEditValid | back to summary |
|---|---|
| private void setEditValid(boolean isValid) Sets the validity of the edit on the receiver. You should not normally
invoke this. This will be invoked by the
Not all formatters will allow the component to get into an invalid state, and thus this may never be invoked. Based on the look and feel this may visually change the state of the receiver.
| |
| setFocusLostBehavior | back to summary |
|---|---|
| public void setFocusLostBehavior(int behavior) Sets the behavior when focus is lost. This will be one of
This will throw an
The default value of this property is
| |
| setFormatter | back to summary |
|---|---|
| protected void setFormatter(JFormattedTextField. Sets the current
You should not normally invoke this, instead set the
This is a JavaBeans bound property.
| |
| setFormatterActions | back to summary |
|---|---|
| private void setFormatterActions(Action[] actions) Resets the Actions that come from the TextFormatter to
| |
| setFormatterFactory | back to summary |
|---|---|
| public void setFormatterFactory(JFormattedTextField. Sets the
If you have not explicitly set an This is a JavaBeans bound property.
| |
| setValue | back to summary |
|---|---|
| public void setValue(Object value) Sets the value that will be formatted by an
The default value of this property is null. This is a JavaBeans bound property.
| |
| setValue | back to summary |
|---|---|
| private void setValue(Object value, boolean createFormat, boolean firePC) Does the setting of the value. If | |
| writeObject | back to summary |
|---|---|
| private void writeObject(ObjectOutputStream s) throws IOException Hides javax. Doc from javax. See
| |
AbstractFormatter are used by
JFormattedTextField to handle the conversion both
from an Object to a String, and back from a String to an Object.
AbstractFormatters can also enforce editing policies,
or navigation policies, or manipulate the
JFormattedTextField in any way it sees fit to
enforce the desired policy.
An AbstractFormatter can only be active in
one JFormattedTextField at a time.
JFormattedTextField invokes
install when it is ready to use it followed
by uninstall when done. Subclasses
that wish to install additional state should override
install and message super appropriately.
Subclasses must override the conversion methods
stringToValue and valueToString. Optionally
they can override getActions,
getNavigationFilter and getDocumentFilter
to restrict the JFormattedTextField in a particular
way.
Subclasses that allow the JFormattedTextField to be in
a temporarily invalid state should invoke setEditValid
at the appropriate times.
| Modifier and Type | Field and Description |
|---|---|
| private JFormattedTextField |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| protected Object | Returns: Copy of the AbstractFormatterOverrides java. Clones the |
| protected Action[] | Returns: Array of Actions to install on JFormattedTextFieldSubclass and override if you wish to provide a custom set of
|
| protected DocumentFilter | Returns: DocumentFilter to restrict editsSubclass and override if you wish to provide a
|
| protected JFormattedTextField | Returns: JFormattedTextField formatting for.Returns the current |
| protected NavigationFilter | Returns: NavigationFilter to restrict navigationSubclass and override if you wish to provide a filter to restrict where the user can navigate to. |
| public void | install(JFormattedTextField
JFormattedTextField to format for, may be null indicating
uninstall from current JFormattedTextField. ftf)Installs the |
| private void | installDocumentFilter(DocumentFilter
DocumentFilter to install on the Document. filter)Installs the |
| protected void | |
| protected void | setEditValid(boolean
Valid state of the JFormattedTextField valid)Invoke this to update the |
| public abstract Object | Returns: Object representation of textString to convert text)Parses |
| public void | uninstall()
Uninstalls any state the |
| public abstract String | Returns: String representation of valueValue to convert value)Returns the string value to display for |
| ftf | back to summary |
|---|---|
| private JFormattedTextField ftf | |
| AbstractFormatter | back to summary |
|---|---|
| protected AbstractFormatter() Constructor for subclasses to call. | |
| clone | back to summary |
|---|---|
| protected Object clone() throws CloneNotSupportedException Overrides java. Clones the
| |
| getActions | back to summary |
|---|---|
| protected Action[] getActions() Subclass and override if you wish to provide a custom set of
| |
| getDocumentFilter | back to summary |
|---|---|
| protected DocumentFilter getDocumentFilter() Subclass and override if you wish to provide a
Implementation Specification The default implementation returns
| |
| getFormattedTextField | back to summary |
|---|---|
| protected JFormattedTextField getFormattedTextField() Returns the current
| |
| getNavigationFilter | back to summary |
|---|---|
| protected NavigationFilter getNavigationFilter() Subclass and override if you wish to provide a filter to restrict
where the user can navigate to.
Implementation Specification The default implementation returns
| |
| install | back to summary |
|---|---|
| public void install(JFormattedTextField ftf) Installs the
Subclasses will typically only need to override this if they
wish to install additional listeners on the
If there is a
While this is a public method, this is typically only useful
for subclassers of
| |
| installDocumentFilter | back to summary |
|---|---|
| private void installDocumentFilter(DocumentFilter filter) Installs the
| |
| invalidEdit | back to summary |
|---|---|
| protected void invalidEdit() This should be invoked when the user types an invalid character. This forwards the call to the current JFormattedTextField. | |
| setEditValid | back to summary |
|---|---|
| protected void setEditValid(boolean valid) Invoke this to update the
| |
| stringToValue | back to summary |
|---|---|
| public abstract Object stringToValue(String text) throws ParseException Parses
| |
| uninstall | back to summary |
|---|---|
| public void uninstall() Uninstalls any state the | |
| valueToString | back to summary |
|---|---|
| public abstract String valueToString(Object value) throws ParseException Returns the string value to display for
| |
AbstractFormatterFactory are used by
JFormattedTextField to obtain instances of
AbstractFormatter which in turn are used to format values.
AbstractFormatterFactory can return different
AbstractFormatters based on the state of the
JFormattedTextField, perhaps returning different
AbstractFormatters when the
JFormattedTextField has focus vs when it
doesn't have focus.
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public abstract JFormattedTextField. | Returns: AbstractFormatter to handle formatting duties, a null return value implies the JFormattedTextField should behave like a normal JTextFieldJFormattedTextField requesting AbstractFormatter tf)Returns an |
| AbstractFormatterFactory | back to summary |
|---|---|
| protected AbstractFormatterFactory() Constructor for subclasses to call. | |
| getFormatter | back to summary |
|---|---|
| public abstract JFormattedTextField. Returns an
| |
actionPerformed is invoked. It will only be
enabled if the focused component is an instance of
JFormattedTextField.
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | actionPerformed(ActionEvent e)
Implements java. Invoked when an action occurs. |
| public boolean | isEnabled()
Overrides javax. Implements javax. Returns true if the action is enabled. |
| CancelAction | back to summary |
|---|---|
| public CancelAction() | |
| actionPerformed | back to summary |
|---|---|
| public void actionPerformed(ActionEvent e) Implements java. Doc from java. Invoked when an action occurs. | |
| isEnabled | back to summary |
|---|---|
| public boolean isEnabled() Overrides javax. Implements javax. Doc from javax. Returns true if the action is enabled. | |
isEnabled is true while a JFormattedTextField
has focus, and extends actionPerformed to invoke
commitEdit.
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void | actionPerformed(ActionEvent e)
Overrides javax. Implements java. Invoked when an action occurs. |
| public boolean | isEnabled()
Overrides javax. Implements javax. Returns true if the action is enabled. |
| CommitAction | back to summary |
|---|---|
| pack-priv CommitAction() | |
| actionPerformed | back to summary |
|---|---|
| public void actionPerformed(ActionEvent e) Overrides javax. Implements java. Doc from java. Invoked when an action occurs. | |
| isEnabled | back to summary |
|---|---|
| public boolean isEnabled() Overrides javax. Implements javax. Doc from javax. Returns true if the action is enabled. | |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | changedUpdate(DocumentEvent e)
Implements javax. Gives notification that an attribute or set of attributes changed. |
| public void | insertUpdate(DocumentEvent e)
Implements javax. Gives notification that there was an insert into the document. |
| public void | removeUpdate(DocumentEvent e)
Implements javax. Gives notification that a portion of the document has been removed. |
| DocumentHandler | back to summary |
|---|---|
| private DocumentHandler() | |
| changedUpdate | back to summary |
|---|---|
| public void changedUpdate(DocumentEvent e) Implements javax. Doc from javax. Gives notification that an attribute or set of attributes changed. | |
| insertUpdate | back to summary |
|---|---|
| public void insertUpdate(DocumentEvent e) Implements javax. Doc from javax. Gives notification that there was an insert into the document. The range given by the DocumentEvent bounds the freshly inserted region. | |
| removeUpdate | back to summary |
|---|---|
| public void removeUpdate(DocumentEvent e) Implements javax. Doc from javax. Gives notification that a portion of the document has been removed. The range is given in terms of what the view last saw (that is, before updating sticky positions). | |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void |
| FocusLostHandler | back to summary |
|---|---|
| private FocusLostHandler() | |
| run | back to summary |
|---|---|
| public void run() Implements java. Doc from java. Runs this operation. | |