APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing

public Class JFormattedTextField

extends JTextField
Class Inheritance
Annotations
@JavaBean
@SuppressWarnings:serial
Imports
java.awt.AWTEvent, .EventQueue, java.awt.event.ActionEvent, .FocusEvent, .InputMethodEvent, java.awt.im.InputContext, java.beans.BeanProperty, .JavaBean, java.io.IOException, .ObjectOutputStream, .Serial, .Serializable, java.text.AttributedCharacterIterator, .DateFormat, .DecimalFormat, .Format, .NumberFormat, .ParseException, java.util.Date, javax.swing.event.DocumentEvent, .DocumentListener, javax.swing.plaf.UIResource, javax.swing.text.AbstractDocument, .DateFormatter, .DefaultFormatter, .DefaultFormatterFactory, .Document, .DocumentFilter, .InternationalFormatter, .JTextComponent, .NavigationFilter, .NumberFormatter, .TextAction

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:

Possible JFormattedTextField configurations and their descriptions
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.
The default is 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.beans.XMLEncoder.

Since
1.4

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract static class
JFormattedTextField.AbstractFormatter

Instances of AbstractFormatter are used by JFormattedTextField to handle the conversion both from an Object to a String, and back from a String to an Object.

public abstract static class
JFormattedTextField.AbstractFormatterFactory

Instances of AbstractFormatterFactory are used by JFormattedTextField to obtain instances of AbstractFormatter which in turn are used to format values.

private static class
JFormattedTextField.CancelAction

CancelAction will reset the value in the JFormattedTextField when actionPerformed is invoked.

pack-priv static class
JFormattedTextField.CommitAction

Used to commit the edit.

private class
JFormattedTextField.DocumentHandler

Sets the dirty state as the document changes.

private class
JFormattedTextField.FocusLostHandler

FOCUS_LOST behavior implementation

Field Summary

Modifier and TypeField and Description
public static final int
COMMIT

Constant identifying that when focus is lost, commitEdit should be invoked.

public static final int
COMMIT_OR_REVERT

Constant identifying that when focus is lost, commitEdit should be invoked.

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.AbstractFormatterFactory
factory

Factory used to obtain an instance of AbstractFormatter.

private int
focusLostBehavior

Behavior when focus is lost.

private JFormattedTextField.FocusLostHandler
focusLostHandler

A handler for FOCUS_LOST event

private JFormattedTextField.AbstractFormatter
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 JFormattedTextField.

private ActionMap
textFormatterActionMap

ActionMap that the TextFormatter Actions are added to.

private static final String
private Object
value

Last valid value.

Inherited from javax.swing.JTextField:
notifyAction

Constructor Summary

AccessConstructor and Description
public
JFormattedTextField()

Creates a JFormattedTextField with no AbstractFormatterFactory.

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 JFormattedTextField.

public
JFormattedTextField(JFormattedTextField.AbstractFormatter
AbstractFormatter to use for formatting.
formatter
)

Creates a JFormattedTextField with the specified AbstractFormatter.

public
JFormattedTextField(JFormattedTextField.AbstractFormatterFactory
AbstractFormatterFactory used for formatting.
factory
)

Creates a JFormattedTextField with the specified AbstractFormatterFactory.

public
JFormattedTextField(JFormattedTextField.AbstractFormatterFactory
AbstractFormatterFactory used for formatting.
factory
,
Object
Initial value to use
currentValue
)

Creates a JFormattedTextField with the specified AbstractFormatterFactory and initial value.

Method Summary

Modifier and TypeMethod and Description
public void
commitEdit()

Forces the current value to be taken from the AbstractFormatter and set as the current value.

public Action[]

Returns:

the command list
getActions
()

Overrides javax.swing.JTextField.getActions.

Fetches the command list for the editor.

private JFormattedTextField.AbstractFormatterFactory
getDefaultFormatterFactory(Object type)

Returns an AbstractFormatterFactory suitable for the passed in Object type.

public int

Returns:

returns behavior when focus is lost
getFocusLostBehavior
()

Returns the behavior when focus is lost.

public JFormattedTextField.AbstractFormatter

Returns:

AbstractFormatter used for formatting
getFormatter
()

Returns the AbstractFormatter that is used to format and parse the current value.

public JFormattedTextField.AbstractFormatterFactory

Returns:

AbstractFormatterFactory used to determine AbstractFormatters
getFormatterFactory
()

Returns the current AbstractFormatterFactory.

public String

Returns:

the string "FormattedTextFieldUI"
getUIClassID
()

Overrides javax.swing.JTextField.getUIClassID.

Gets the class ID for a UI.

public Object

Returns:

Last valid value
getValue
()

Returns the last valid value.

protected void
invalidEdit()

Invoked when the user inputs an invalid value.

private boolean
isEdited()

Returns true if the receiver has been edited.

public boolean

Returns:

true if the current value being edited is valid.
isEditValid
()

Returns true if the current value being edited is valid.

protected void
processFocusEvent(FocusEvent
the FocusEvent
e
)

Overrides java.awt.Component.processFocusEvent.

Processes any focus events, such as FocusEvent.FOCUS_GAINED or FocusEvent.FOCUS_LOST.

protected void
processInputMethodEvent(InputMethodEvent
the InputMethodEvent
e
)

Overrides javax.swing.text.JTextComponent.processInputMethodEvent.

Processes any input method events, such as InputMethodEvent.INPUT_METHOD_TEXT_CHANGED or InputMethodEvent.CARET_POSITION_CHANGED.

public void
setDocument(Document
the document to display/edit
doc
)

Overrides javax.swing.JTextField.setDocument.

Associates the editor with a text document.

private void
setEdited(boolean edited)

Sets the edited state of the receiver.

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
AbstractFormatter to use for formatting
format
)

Sets the current AbstractFormatter.

private void
setFormatterActions(Action[] actions)

Resets the Actions that come from the TextFormatter to actions.

public void
setFormatterFactory(JFormattedTextField.AbstractFormatterFactory
AbstractFormatterFactory used to lookup instances of AbstractFormatter
tf
)

Sets the AbstractFormatterFactory.

public void
setValue(Object
Current value to display
value
)

Sets the value that will be formatted by an AbstractFormatter obtained from the current AbstractFormatterFactory.

private void
setValue(Object value, boolean createFormat, boolean firePC)

Does the setting of the value.

private void
writeObject(ObjectOutputStream s)

Hides javax.swing.JTextField.writeObject.

See readObject and writeObject in JComponent for more information about serialization in Swing.

Inherited from javax.swing.JTextField:
actionPropertyChangedaddActionListenerconfigurePropertiesFromActioncreateActionPropertyChangeListenercreateDefaultModelfireActionPerformedgetAccessibleContextgetActiongetActionListenersgetColumnsgetColumnWidthgetHorizontalAlignmentgetHorizontalVisibilitygetPreferredSizegetScrollOffsethasActionListenerisValidateRootparamStringpostActionEventremoveActionListenerscrollRectToVisiblesetActionsetActionCommandsetColumnssetFontsetHorizontalAlignmentsetScrollOffset

Field Detail

COMMITback to summary
public static final int COMMIT

Constant identifying that when focus is lost, commitEdit should be invoked. If in committing the new value a ParseException is thrown, the invalid value will remain.

See Also
setFocusLostBehavior
COMMIT_OR_REVERTback to summary
public static final int COMMIT_OR_REVERT

Constant identifying that when focus is lost, commitEdit should be invoked. If in committing the new value a ParseException is thrown, the value will be reverted.

See Also
setFocusLostBehavior
composedTextExistsback to summary
private boolean composedTextExists

Indicates the input method composed text is in the document

defaultActionsback to summary
private static final Action[] defaultActions

Hides javax.swing.JTextField.defaultActions.

documentListenerback to summary
private DocumentListener documentListener

Used to set the dirty state.

editedback to summary
private boolean edited

Indicates the current value has been edited.

editValidback to summary
private boolean editValid

True while the value being edited is valid.

factoryback to summary
private JFormattedTextField.AbstractFormatterFactory factory

Factory used to obtain an instance of AbstractFormatter.

focusLostBehaviorback to summary
private int focusLostBehavior

Behavior when focus is lost.

focusLostHandlerback to summary
private JFormattedTextField.FocusLostHandler focusLostHandler

A handler for FOCUS_LOST event

formatback to summary
private JFormattedTextField.AbstractFormatter format

Object responsible for formatting the current value.

PERSISTback to summary
public static final int PERSIST

Constant identifying that when focus is lost, the edited value should be left.

See Also
setFocusLostBehavior
REVERTback 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 JFormattedTextField.

See Also
setFocusLostBehavior
textFormatterActionMapback to summary
private ActionMap textFormatterActionMap

ActionMap that the TextFormatter Actions are added to.

uiClassIDback to summary
private static final String uiClassID

Hides javax.swing.JTextField.uiClassID.

valueback to summary
private Object value

Last valid value.

Constructor Detail

JFormattedTextFieldback to summary
public JFormattedTextField()

Creates a JFormattedTextField with no AbstractFormatterFactory. Use setMask or setFormatterFactory to configure the JFormattedTextField to edit a particular type of value.

JFormattedTextFieldback to summary
public JFormattedTextField(Object value)

Creates a JFormattedTextField with the specified value. This will create an AbstractFormatterFactory based on the type of value.

Parameters
value:Object

Initial value for the JFormattedTextField

JFormattedTextFieldback to summary
public JFormattedTextField(Format format)

Creates a JFormattedTextField. format is wrapped in an appropriate AbstractFormatter which is then wrapped in an AbstractFormatterFactory.

Parameters
format:Format

Format used to look up an AbstractFormatter

JFormattedTextFieldback to summary
public JFormattedTextField(JFormattedTextField.AbstractFormatter formatter)

Creates a JFormattedTextField with the specified AbstractFormatter. The AbstractFormatter is placed in an AbstractFormatterFactory.

Parameters
formatter:JFormattedTextField.AbstractFormatter

AbstractFormatter to use for formatting.

JFormattedTextFieldback to summary
public JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory)

Creates a JFormattedTextField with the specified AbstractFormatterFactory.

Parameters
factory:JFormattedTextField.AbstractFormatterFactory

AbstractFormatterFactory used for formatting.

JFormattedTextFieldback to summary
public JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory, Object currentValue)

Creates a JFormattedTextField with the specified AbstractFormatterFactory and initial value.

Parameters
factory:JFormattedTextField.AbstractFormatterFactory

AbstractFormatterFactory used for formatting.

currentValue:Object

Initial value to use

Method Detail

commitEditback to summary
public void commitEdit() throws ParseException

Forces the current value to be taken from the AbstractFormatter and set as the current value. This has no effect if there is no current AbstractFormatter installed.

Exceptions
ParseException:
if the AbstractFormatter is not able to format the current value
getActionsback to summary
public Action[] getActions()

Overrides javax.swing.JTextField.getActions.

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.

Returns:Action[]

the command list

Annotations
@BeanProperty
bound:false
getDefaultFormatterFactoryback to summary
private JFormattedTextField.AbstractFormatterFactory getDefaultFormatterFactory(Object type)

Returns an AbstractFormatterFactory suitable for the passed in Object type.

getFocusLostBehaviorback to summary
public int getFocusLostBehavior()

Returns the behavior when focus is lost. This will be one of COMMIT_OR_REVERT, COMMIT, REVERT or PERSIST Note that some AbstractFormatters may push changes as they occur, so that the value of this will have no effect.

Returns:int

returns behavior when focus is lost

getFormatterback to summary
public JFormattedTextField.AbstractFormatter getFormatter()

Returns the AbstractFormatter that is used to format and parse the current value.

Returns:JFormattedTextField.AbstractFormatter

AbstractFormatter used for formatting

Annotations
@BeanProperty
visualUpdate:true
description:TextFormatter, responsible for formatting the current value
getFormatterFactoryback to summary
public JFormattedTextField.AbstractFormatterFactory getFormatterFactory()

Returns the current AbstractFormatterFactory.

Returns:JFormattedTextField.AbstractFormatterFactory

AbstractFormatterFactory used to determine AbstractFormatters

See Also
setFormatterFactory
getUIClassIDback to summary
public String getUIClassID()

Overrides javax.swing.JTextField.getUIClassID.

Gets the class ID for a UI.

Returns:String

the string "FormattedTextFieldUI"

Annotations
@BeanProperty
bound:false
See Also
JComponent#getUIClassID
getValueback to summary
public Object getValue()

Returns the last valid value. Based on the editing policy of the AbstractFormatter this may not return the current value. The currently edited value can be obtained by invoking commitEdit followed by getValue.

Returns:Object

Last valid value

invalidEditback 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.

isEditedback to summary
private boolean isEdited()

Returns true if the receiver has been edited.

isEditValidback to summary
public boolean isEditValid()

Returns true if the current value being edited is valid. The value of this is managed by the current AbstractFormatter, as such there is no public setter for it.

Returns:boolean

true if the current value being edited is valid.

Annotations
@BeanProperty
bound:false
processFocusEventback to summary
protected void processFocusEvent(FocusEvent e)

Overrides java.awt.Component.processFocusEvent.

Processes any focus events, such as FocusEvent.FOCUS_GAINED or FocusEvent.FOCUS_LOST.

Parameters
e:FocusEvent

the FocusEvent

See Also
FocusEvent
processInputMethodEventback to summary
protected void processInputMethodEvent(InputMethodEvent e)

Overrides javax.swing.text.JTextComponent.processInputMethodEvent.

Processes any input method events, such as InputMethodEvent.INPUT_METHOD_TEXT_CHANGED or InputMethodEvent.CARET_POSITION_CHANGED.

Parameters
e:InputMethodEvent

the InputMethodEvent

See Also
InputMethodEvent
setDocumentback to summary
public void setDocument(Document doc)

Overrides javax.swing.JTextField.setDocument.

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.

Parameters
doc:Document

the document to display/edit

Annotations
@BeanProperty
expert:true
description:the text document model
See Also
getDocument
setEditedback to summary
private void setEdited(boolean edited)

Sets the edited state of the receiver.

setEditValidback 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 AbstractFormatter as the user edits the value.

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.

Parameters
isValid:boolean

boolean indicating if the currently edited value is valid.

Annotations
@BeanProperty
visualUpdate:true
description:True indicates the edited value is valid
setFocusLostBehaviorback to summary
public void setFocusLostBehavior(int behavior)

Sets the behavior when focus is lost. This will be one of JFormattedTextField.COMMIT_OR_REVERT, JFormattedTextField.REVERT, JFormattedTextField.COMMIT or JFormattedTextField.PERSIST Note that some AbstractFormatters may push changes as they occur, so that the value of this will have no effect.

This will throw an IllegalArgumentException if the object passed in is not one of the afore mentioned values.

The default value of this property is JFormattedTextField.COMMIT_OR_REVERT.

Parameters
behavior:int

Identifies behavior when focus is lost

Annotations
@BeanProperty
bound:false
enumerationValues:JFormattedTextField.COMMIT, JFormattedTextField.COMMIT_OR_REVERT, JFormattedTextField.REVERT, JFormattedTextField.PERSIST
description:Behavior when component loses focus
Exceptions
IllegalArgumentException:
if behavior is not one of the known values
setFormatterback to summary
protected void setFormatter(JFormattedTextField.AbstractFormatter format)

Sets the current AbstractFormatter.

You should not normally invoke this, instead set the AbstractFormatterFactory or set the value. JFormattedTextField will invoke this as the state of the JFormattedTextField changes and requires the value to be reset. JFormattedTextField passes in the AbstractFormatter obtained from the AbstractFormatterFactory.

This is a JavaBeans bound property.

Parameters
format:JFormattedTextField.AbstractFormatter

AbstractFormatter to use for formatting

See Also
setFormatterFactory
setFormatterActionsback to summary
private void setFormatterActions(Action[] actions)

Resets the Actions that come from the TextFormatter to actions.

setFormatterFactoryback to summary
public void setFormatterFactory(JFormattedTextField.AbstractFormatterFactory tf)

Sets the AbstractFormatterFactory. AbstractFormatterFactory is able to return an instance of AbstractFormatter that is used to format a value for display, as well an enforcing an editing policy.

If you have not explicitly set an AbstractFormatterFactory by way of this method (or a constructor) an AbstractFormatterFactory and consequently an AbstractFormatter will be used based on the Class of the value. NumberFormatter will be used for Numbers, DateFormatter will be used for Dates, otherwise DefaultFormatter will be used.

This is a JavaBeans bound property.

Parameters
tf:JFormattedTextField.AbstractFormatterFactory

AbstractFormatterFactory used to lookup instances of AbstractFormatter

Annotations
@BeanProperty
visualUpdate:true
description:AbstractFormatterFactory, responsible for returning an AbstractFormatter that can format the current value.
setValueback to summary
public void setValue(Object value)

Sets the value that will be formatted by an AbstractFormatter obtained from the current AbstractFormatterFactory. If no AbstractFormatterFactory has been specified, this will attempt to create one based on the type of value.

The default value of this property is null.

This is a JavaBeans bound property.

Parameters
value:Object

Current value to display

Annotations
@BeanProperty
visualUpdate:true
description:The value to be formatted.
setValueback to summary
private void setValue(Object value, boolean createFormat, boolean firePC)

Does the setting of the value. If createFormat is true, this will also obtain a new AbstractFormatter from the current factory. The property change event will be fired if firePC is true.

writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException

Hides javax.swing.JTextField.writeObject.

Doc from javax.swing.JTextField.writeObject.

See readObject and writeObject in JComponent for more information about serialization in Swing.

Annotations
@Serial
Exceptions
IOException:

Doc from java.awt.Container.writeObject.

if an I/O error occurs

javax.swing back to summary

public abstract Class JFormattedTextField.AbstractFormatter

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Known Direct Subclasses
javax.swing.JSpinner.ListEditor.ListFormatter, javax.swing.colorchooser.ValueFormatter, javax.swing.text.DefaultFormatter

Instances of 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.

Since
1.4

Field Summary

Modifier and TypeField and Description
private JFormattedTextField

Constructor Summary

AccessConstructor and Description
protected
AbstractFormatter()

Constructor for subclasses to call.

Method Summary

Modifier and TypeMethod and Description
protected Object

Returns:

Copy of the AbstractFormatter
clone
()

Overrides java.lang.Object.clone.

Clones the AbstractFormatter.

protected Action[]

Returns:

Array of Actions to install on JFormattedTextField
getActions
()

Subclass and override if you wish to provide a custom set of Actions.

protected DocumentFilter

Returns:

DocumentFilter to restrict edits
getDocumentFilter
()

Subclass and override if you wish to provide a DocumentFilter to restrict what can be input.

protected JFormattedTextField

Returns:

JFormattedTextField formatting for.
getFormattedTextField
()

Returns the current JFormattedTextField the AbstractFormatter is installed on.

protected NavigationFilter

Returns:

NavigationFilter to restrict navigation
getNavigationFilter
()

Subclass 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 AbstractFormatter onto a particular JFormattedTextField.

private void
installDocumentFilter(DocumentFilter
DocumentFilter to install on the Document.
filter
)

Installs the DocumentFilter filter onto the current JFormattedTextField.

protected void
invalidEdit()

This should be invoked when the user types an invalid character.

protected void
setEditValid(boolean
Valid state of the JFormattedTextField
valid
)

Invoke this to update the editValid property of the JFormattedTextField.

public abstract Object

Returns:

Object representation of text
stringToValue
(String
String to convert
text
)

Parses text returning an arbitrary Object.

public void
uninstall()

Uninstalls any state the AbstractFormatter may have installed on the JFormattedTextField.

public abstract String

Returns:

String representation of value
valueToString
(Object
Value to convert
value
)

Returns the string value to display for value.

Inherited from java.lang.Object:
equalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ftfback to summary
private JFormattedTextField ftf

Constructor Detail

AbstractFormatterback to summary
protected AbstractFormatter()

Constructor for subclasses to call.

Method Detail

cloneback to summary
protected Object clone() throws CloneNotSupportedException

Overrides java.lang.Object.clone.

Clones the AbstractFormatter. The returned instance is not associated with a JFormattedTextField.

Returns:Object

Copy of the AbstractFormatter

Exceptions
CloneNotSupportedException:

Doc from java.lang.Object.clone.

if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.

getActionsback to summary
protected Action[] getActions()

Subclass and override if you wish to provide a custom set of Actions. install will install these on the JFormattedTextField's ActionMap.

Returns:Action[]

Array of Actions to install on JFormattedTextField

getDocumentFilterback to summary
protected DocumentFilter getDocumentFilter()

Subclass and override if you wish to provide a DocumentFilter to restrict what can be input. install will install the returned value onto the JFormattedTextField.

Implementation Specification

The default implementation returns null.

Returns:DocumentFilter

DocumentFilter to restrict edits

getFormattedTextFieldback to summary
protected JFormattedTextField getFormattedTextField()

Returns the current JFormattedTextField the AbstractFormatter is installed on.

Returns:JFormattedTextField

JFormattedTextField formatting for.

getNavigationFilterback to summary
protected NavigationFilter getNavigationFilter()

Subclass and override if you wish to provide a filter to restrict where the user can navigate to. install will install the returned value onto the JFormattedTextField.

Implementation Specification

The default implementation returns null.

Returns:NavigationFilter

NavigationFilter to restrict navigation

installback to summary
public void install(JFormattedTextField ftf)

Installs the AbstractFormatter onto a particular JFormattedTextField. This will invoke valueToString to convert the current value from the JFormattedTextField to a String. This will then install the Actions from getActions, the DocumentFilter returned from getDocumentFilter and the NavigationFilter returned from getNavigationFilter onto the JFormattedTextField.

Subclasses will typically only need to override this if they wish to install additional listeners on the JFormattedTextField.

If there is a ParseException in converting the current value to a String, this will set the text to an empty String, and mark the JFormattedTextField as being in an invalid state.

While this is a public method, this is typically only useful for subclassers of JFormattedTextField. JFormattedTextField will invoke this method at the appropriate times when the value changes, or its internal state changes. You will only need to invoke this yourself if you are subclassing JFormattedTextField and installing/uninstalling AbstractFormatter at a different time than JFormattedTextField does.

Parameters
ftf:JFormattedTextField

JFormattedTextField to format for, may be null indicating uninstall from current JFormattedTextField.

installDocumentFilterback to summary
private void installDocumentFilter(DocumentFilter filter)

Installs the DocumentFilter filter onto the current JFormattedTextField.

Parameters
filter:DocumentFilter

DocumentFilter to install on the Document.

invalidEditback to summary
protected void invalidEdit()

This should be invoked when the user types an invalid character. This forwards the call to the current JFormattedTextField.

setEditValidback to summary
protected void setEditValid(boolean valid)

Invoke this to update the editValid property of the JFormattedTextField. If you an enforce a policy such that the JFormattedTextField is always in a valid state, you will never need to invoke this.

Parameters
valid:boolean

Valid state of the JFormattedTextField

stringToValueback to summary
public abstract Object stringToValue(String text) throws ParseException

Parses text returning an arbitrary Object. Some formatters may return null.

Parameters
text:String

String to convert

Returns:Object

Object representation of text

Exceptions
ParseException:
if there is an error in the conversion
uninstallback to summary
public void uninstall()

Uninstalls any state the AbstractFormatter may have installed on the JFormattedTextField. This resets the DocumentFilter, NavigationFilter and additional Actions installed on the JFormattedTextField.

valueToStringback to summary
public abstract String valueToString(Object value) throws ParseException

Returns the string value to display for value.

Parameters
value:Object

Value to convert

Returns:String

String representation of value

Exceptions
ParseException:
if there is an error in the conversion
javax.swing back to summary

public abstract Class JFormattedTextField.AbstractFormatterFactory

extends Object
Class Inheritance
Known Direct Subclasses
javax.swing.text.DefaultFormatterFactory

Instances of 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.
Since
1.4

Constructor Summary

AccessConstructor and Description
protected
AbstractFormatterFactory()

Constructor for subclasses to call.

Method Summary

Modifier and TypeMethod and Description
public abstract JFormattedTextField.AbstractFormatter

Returns:

AbstractFormatter to handle formatting duties, a null return value implies the JFormattedTextField should behave like a normal JTextField
getFormatter
(JFormattedTextField
JFormattedTextField requesting AbstractFormatter
tf
)

Returns an AbstractFormatter that can handle formatting of the passed in JFormattedTextField.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

AbstractFormatterFactoryback to summary
protected AbstractFormatterFactory()

Constructor for subclasses to call.

Method Detail

getFormatterback to summary
public abstract JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField tf)

Returns an AbstractFormatter that can handle formatting of the passed in JFormattedTextField.

Parameters
tf:JFormattedTextField

JFormattedTextField requesting AbstractFormatter

Returns:JFormattedTextField.AbstractFormatter

AbstractFormatter to handle formatting duties, a null return value implies the JFormattedTextField should behave like a normal JTextField

javax.swing back to summary

private Class JFormattedTextField.CancelAction

extends TextAction
Class Inheritance

CancelAction will reset the value in the JFormattedTextField when actionPerformed is invoked. It will only be enabled if the focused component is an instance of JFormattedTextField.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
actionPerformed(ActionEvent e)

Implements java.awt.event.ActionListener.actionPerformed.

Invoked when an action occurs.

public boolean
isEnabled()

Overrides javax.swing.AbstractAction.isEnabled.

Implements javax.swing.Action.isEnabled.

Returns true if the action is enabled.

Inherited from javax.swing.text.TextAction:
augmentListgetFocusedComponentgetTextComponent

Constructor Detail

CancelActionback to summary
public CancelAction()

Method Detail

actionPerformedback to summary
public void actionPerformed(ActionEvent e)

Implements java.awt.event.ActionListener.actionPerformed.

Doc from java.awt.event.ActionListener.actionPerformed.

Invoked when an action occurs.

isEnabledback to summary
public boolean isEnabled()

Overrides javax.swing.AbstractAction.isEnabled.

Implements javax.swing.Action.isEnabled.

Doc from javax.swing.AbstractAction.isEnabled.

Returns true if the action is enabled.

javax.swing back to summary

pack-priv Class JFormattedTextField.CommitAction

extends NotifyAction
Class Inheritance

Used to commit the edit. This extends JTextField.NotifyAction so that isEnabled is true while a JFormattedTextField has focus, and extends actionPerformed to invoke commitEdit.

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
public boolean
isEnabled()

Overrides javax.swing.JTextField.NotifyAction.isEnabled.

Implements javax.swing.Action.isEnabled.

Returns true if the action is enabled.

Constructor Detail

CommitActionback to summary
pack-priv CommitAction()

Method Detail

actionPerformedback to summary
public void actionPerformed(ActionEvent e)

Overrides javax.swing.JTextField.NotifyAction.actionPerformed.

Implements java.awt.event.ActionListener.actionPerformed.

Doc from java.awt.event.ActionListener.actionPerformed.

Invoked when an action occurs.

isEnabledback to summary
public boolean isEnabled()

Overrides javax.swing.JTextField.NotifyAction.isEnabled.

Implements javax.swing.Action.isEnabled.

Doc from javax.swing.AbstractAction.isEnabled.

Returns true if the action is enabled.

javax.swing back to summary

private Class JFormattedTextField.DocumentHandler

extends Object
implements DocumentListener, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, javax.swing.event.DocumentListener, java.util.EventListener

Sets the dirty state as the document changes.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
changedUpdate(DocumentEvent e)

Implements javax.swing.event.DocumentListener.changedUpdate.

Gives notification that an attribute or set of attributes changed.

public void
insertUpdate(DocumentEvent e)

Implements javax.swing.event.DocumentListener.insertUpdate.

Gives notification that there was an insert into the document.

public void
removeUpdate(DocumentEvent e)

Implements javax.swing.event.DocumentListener.removeUpdate.

Gives notification that a portion of the document has been removed.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

DocumentHandlerback to summary
private DocumentHandler()

Method Detail

changedUpdateback to summary
public void changedUpdate(DocumentEvent e)

Implements javax.swing.event.DocumentListener.changedUpdate.

Doc from javax.swing.event.DocumentListener.changedUpdate.

Gives notification that an attribute or set of attributes changed.

insertUpdateback to summary
public void insertUpdate(DocumentEvent e)

Implements javax.swing.event.DocumentListener.insertUpdate.

Doc from javax.swing.event.DocumentListener.insertUpdate.

Gives notification that there was an insert into the document. The range given by the DocumentEvent bounds the freshly inserted region.

removeUpdateback to summary
public void removeUpdate(DocumentEvent e)

Implements javax.swing.event.DocumentListener.removeUpdate.

Doc from javax.swing.event.DocumentListener.removeUpdate.

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).

javax.swing back to summary

private Class JFormattedTextField.FocusLostHandler

extends Object
implements Runnable, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Runnable

FOCUS_LOST behavior implementation

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

FocusLostHandlerback to summary
private FocusLostHandler()

Method Detail

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.