javax.swing.text

Class DefaultFormatter

public class DefaultFormatter extends JFormattedTextField.AbstractFormatter implements Cloneable, Serializable

The DefaultFormatter is a concrete formatter for use in {@link JFormattedTextField}s. It can format arbitrary values by invoking their {@link Object#toString} method. In order to convert a String back to a value, the value class must provide a single argument constructor that takes a String object as argument value. If no such constructor is found, the String itself is passed back by #stringToValue.
Constructor Summary
DefaultFormatter()
Creates a new instance of DefaultFormatter.
Method Summary
Objectclone()
Creates and returns a clone of this DefaultFormatter.
booleangetAllowsInvalid()
Returns whether or not invalid edits are allowed or not.
booleangetCommitsOnValidEdit()
Returns true if the value should be committed after each valid modification of the input field, false if it should never be committed by this formatter.
protected DocumentFiltergetDocumentFilter()
Returns the DocumentFilter that is used to restrict input.
booleangetOverwriteMode()
Returns the value of the overwriteMode property.
Class<?>getValueClass()
Returns the class that is used for values.
voidinstall(JFormattedTextField ftf)
Installs the formatter on the specified {@link JFormattedTextField}.
voidsetAllowsInvalid(boolean allowsInvalid)
Sets the value of the allowsInvalid property.
voidsetCommitsOnValidEdit(boolean commitsOnValidEdit)
Sets the value of the commitsOnValidEdit property.
voidsetOverwriteMode(boolean overwriteMode)
Sets the value of the overwriteMode property.
voidsetValueClass(Class<?> valueClass)
Sets the class that is used for values.
ObjectstringToValue(String string)
Converts a String (from the JFormattedTextField input) to a value.
StringvalueToString(Object value)
Converts a value object into a String.

Constructor Detail

DefaultFormatter

public DefaultFormatter()
Creates a new instance of DefaultFormatter.

Method Detail

clone

public Object clone()
Creates and returns a clone of this DefaultFormatter.

Returns: a clone of this object

Throws: CloneNotSupportedException not thrown here

getAllowsInvalid

public boolean getAllowsInvalid()
Returns whether or not invalid edits are allowed or not. If invalid edits are allowed, the JFormattedTextField may temporarily contain invalid characters.

Returns: the value of the allowsInvalid property

getCommitsOnValidEdit

public boolean getCommitsOnValidEdit()
Returns true if the value should be committed after each valid modification of the input field, false if it should never be committed by this formatter.

Returns: the state of the commitsOnValidEdit property

See Also: DefaultFormatter

getDocumentFilter

protected DocumentFilter getDocumentFilter()
Returns the DocumentFilter that is used to restrict input.

Returns: the DocumentFilter that is used to restrict input

getOverwriteMode

public boolean getOverwriteMode()
Returns the value of the overwriteMode property. If that is set to true then newly inserted characters overwrite existing values, otherwise the characters are inserted like normal. The default is true.

Returns: the value of the overwriteMode property

getValueClass

public Class<?> getValueClass()
Returns the class that is used for values. When Strings are converted back to values, this class is used to create new value objects.

Returns: the class that is used for values

install

public void install(JFormattedTextField ftf)
Installs the formatter on the specified {@link JFormattedTextField}. This method does the following things: This method is typically not overridden by subclasses. Instead override one of the mentioned methods in order to customize behaviour.

Parameters: ftf the {@link JFormattedTextField} in which this formatter is installed

setAllowsInvalid

public void setAllowsInvalid(boolean allowsInvalid)
Sets the value of the allowsInvalid property.

Parameters: allowsInvalid the new value for the property

See Also: getAllowsInvalid

setCommitsOnValidEdit

public void setCommitsOnValidEdit(boolean commitsOnValidEdit)
Sets the value of the commitsOnValidEdit property.

Parameters: commitsOnValidEdit the new state of the commitsOnValidEdit property

See Also: DefaultFormatter

setOverwriteMode

public void setOverwriteMode(boolean overwriteMode)
Sets the value of the overwriteMode property. If that is set to true then newly inserted characters overwrite existing values, otherwise the characters are inserted like normal. The default is true.

Parameters: overwriteMode the new value for the overwriteMode property

setValueClass

public void setValueClass(Class<?> valueClass)
Sets the class that is used for values.

Parameters: valueClass the class that is used for values

See Also: getValueClass

stringToValue

public Object stringToValue(String string)
Converts a String (from the JFormattedTextField input) to a value. In order to achieve this, the formatter tries to instantiate an object of the class returned by #getValueClass() using a single argument constructor that takes a String argument. If such a constructor cannot be found, the String itself is returned.

Parameters: string the string to convert

Returns: the value for the string

Throws: ParseException if the string cannot be converted into a value object (e.g. invalid input)

valueToString

public String valueToString(Object value)
Converts a value object into a String. This is done by invoking the {@link Object#toString()} method on the value.

Parameters: value the value to be converted

Returns: the string representation of the value

Throws: ParseException if the value cannot be converted