javax.swing

Class JFormattedTextField

public class JFormattedTextField extends JTextField

A text field that makes use of a formatter to display and edit a specific type of data. The value that is displayed can be an arbitrary object. The formatter is responsible for displaying the value in a textual form and it may allow editing of the value. Formatters are usually obtained using an instance of {@link AbstractFormatterFactory}. This factory is responsible for providing an instance of {@link AbstractFormatter} that is able to handle the formatting of the value of the JFormattedTextField.

Since: 1.4

Nested Class Summary
abstract static classJFormattedTextField.AbstractFormatter
An abstract base implementation for a formatter that can be used by a JTextField.
abstract static classJFormattedTextField.AbstractFormatterFactory
Delivers instances of an {@link AbstractFormatter} for a specific value type for a JFormattedTextField.
Field Summary
static intCOMMIT
The possible focusLostBehavior options *
static intCOMMIT_OR_REVERT
static intPERSIST
static intREVERT
Constructor Summary
JFormattedTextField()
Creates a JFormattedTextField with no formatter factory.
JFormattedTextField(Format format)
Creates a JFormattedTextField that can handle the specified Format.
JFormattedTextField(JFormattedTextField.AbstractFormatter formatter)
Creates a JFormattedTextField with the specified formatter.
JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory)
Creates a JFormattedTextField with the specified formatter factory.
JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory, Object value)
Creates a JFormattedTextField with the specified formatter factory and initial value.
JFormattedTextField(Object value)
Creates a JFormattedTextField with the specified value.
Method Summary
voidcommitEdit()
Forces the current value from the editor to be set as the current value.
Action[]getActions()
Gets the command list supplied by the UI augmented by the specific Actions for JFormattedTextField.
intgetFocusLostBehavior()
Returns the behaviour of this JFormattedTextField upon losing focus.
JFormattedTextField.AbstractFormattergetFormatter()
Returns the current formatter used for this JFormattedTextField.
JFormattedTextField.AbstractFormatterFactorygetFormatterFactory()
Returns the factory currently used to generate formatters for this JFormattedTextField.
StringgetUIClassID()
ObjectgetValue()
Returns the last valid value.
protected voidinvalidEdit()
This method is used to provide feedback to the user when an invalid value is input during editing.
booleanisEditValid()
Returns true if the current value being edited is valid.
protected voidprocessFocusEvent(FocusEvent evt)
Processes focus events.
voidsetDocument(Document newDocument)
Associates this JFormattedTextField with a Document and propagates a PropertyChange event to each listener.
voidsetFocusLostBehavior(int behavior)
Sets the behaviour of this JFormattedTextField upon losing focus.
protected voidsetFormatter(JFormattedTextField.AbstractFormatter formatter)
Sets the formatter for this JFormattedTextField.
voidsetFormatterFactory(JFormattedTextField.AbstractFormatterFactory factory)
Sets the factory from which this JFormattedTextField should obtain its formatters.
voidsetValue(Object newValue)
Sets the value that will be formatted and displayed.

Field Detail

COMMIT

public static final int COMMIT
The possible focusLostBehavior options *

COMMIT_OR_REVERT

public static final int COMMIT_OR_REVERT

PERSIST

public static final int PERSIST

REVERT

public static final int REVERT

Constructor Detail

JFormattedTextField

public JFormattedTextField()
Creates a JFormattedTextField with no formatter factory. setValue or setFormatterFactory will properly configure this text field to edit a particular type of value.

JFormattedTextField

public JFormattedTextField(Format format)
Creates a JFormattedTextField that can handle the specified Format. An appopriate AbstractFormatter and AbstractFormatterFactory will be created for the specified Format.

Parameters: format the Format that this JFormattedTextField should be able to handle

JFormattedTextField

public JFormattedTextField(JFormattedTextField.AbstractFormatter formatter)
Creates a JFormattedTextField with the specified formatter. This will create a {@link DefaultFormatterFactory} with this formatter as the default formatter.

Parameters: formatter the formatter to use for this JFormattedTextField

JFormattedTextField

public JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory)
Creates a JFormattedTextField with the specified formatter factory.

Parameters: factory the formatter factory to use for this JFormattedTextField

JFormattedTextField

public JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory, Object value)
Creates a JFormattedTextField with the specified formatter factory and initial value.

Parameters: factory the initial formatter factory for this JFormattedTextField value the initial value for the text field

JFormattedTextField

public JFormattedTextField(Object value)
Creates a JFormattedTextField with the specified value. This creates a formatter and formatterFactory that are appropriate for the value.

Parameters: value the initial value for this JFormattedTextField

Method Detail

commitEdit

public void commitEdit()
Forces the current value from the editor to be set as the current value. If there is no current formatted this has no effect.

Throws: ParseException if the formatter cannot format the current value

getActions

public Action[] getActions()
Gets the command list supplied by the UI augmented by the specific Actions for JFormattedTextField.

Returns: an array of Actions that this text field supports

getFocusLostBehavior

public int getFocusLostBehavior()
Returns the behaviour of this JFormattedTextField upon losing focus. This is one of COMMIT, COMMIT_OR_REVERT, PERSIST, or REVERT.

Returns: the behaviour upon losing focus

getFormatter

public JFormattedTextField.AbstractFormatter getFormatter()
Returns the current formatter used for this JFormattedTextField.

Returns: the current formatter used for this JFormattedTextField

getFormatterFactory

public JFormattedTextField.AbstractFormatterFactory getFormatterFactory()
Returns the factory currently used to generate formatters for this JFormattedTextField.

Returns: the factory currently used to generate formatters

getUIClassID

public String getUIClassID()

getValue

public Object getValue()
Returns the last valid value. This may not be the value currently shown in the text field depending on whether or not the formatter commits on valid edits and allows invalid input to be temporarily displayed.

Returns: the last committed valid value

invalidEdit

protected void invalidEdit()
This method is used to provide feedback to the user when an invalid value is input during editing.

isEditValid

public boolean isEditValid()
Returns true if the current value being edited is valid. This property is managed by the current formatted.

Returns: true if the value being edited is valid.

processFocusEvent

protected void processFocusEvent(FocusEvent evt)
Processes focus events. This is overridden because we may want to change the formatted depending on whether or not this field has focus.

Parameters: evt the FocusEvent

setDocument

public void setDocument(Document newDocument)
Associates this JFormattedTextField with a Document and propagates a PropertyChange event to each listener.

Parameters: newDocument the Document to associate with this text field

setFocusLostBehavior

public void setFocusLostBehavior(int behavior)
Sets the behaviour of this JFormattedTextField upon losing focus. This must be COMMIT, COMMIT_OR_REVERT, PERSIST, or REVERT or an IllegalArgumentException will be thrown.

Parameters: behavior

Throws: IllegalArgumentException if behaviour is not one of the above

setFormatter

protected void setFormatter(JFormattedTextField.AbstractFormatter formatter)
Sets the formatter for this JFormattedTextField. Normally the formatter factory will take care of this, or calls to setValue will also make sure that the formatter is set appropriately.

Parameters: formatter the AbstractFormatter to use for formatting the value for this JFormattedTextField

setFormatterFactory

public void setFormatterFactory(JFormattedTextField.AbstractFormatterFactory factory)
Sets the factory from which this JFormattedTextField should obtain its formatters.

Parameters: factory the AbstractFormatterFactory that will be used to generate formatters for this JFormattedTextField

setValue

public void setValue(Object newValue)
Sets the value that will be formatted and displayed.

Parameters: newValue the value to be formatted and displayed