javax.swing.text
Class DefaultFormatter

java.lang.Object
  extended by javax.swing.JFormattedTextField.AbstractFormatter
      extended by javax.swing.text.DefaultFormatter
All Implemented Interfaces:
Serializable, Cloneable
Direct Known Subclasses:
InternationalFormatter, MaskFormatter

public class DefaultFormatter
extends JFormattedTextField.AbstractFormatter
implements Cloneable, Serializable

The DefaultFormatter is a concrete formatter for use in JFormattedTextFields. It can format arbitrary values by invoking their 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.

See Also:
Serialized Form

Constructor Summary
DefaultFormatter()
          Creates a new instance of DefaultFormatter.
 
Method Summary
 Object clone()
          Creates and returns a clone of this DefaultFormatter.
 boolean getAllowsInvalid()
          Returns whether or not invalid edits are allowed or not.
 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.
protected  DocumentFilter getDocumentFilter()
          Returns the DocumentFilter that is used to restrict input.
 boolean getOverwriteMode()
          Returns the value of the overwriteMode property.
 Class<?> getValueClass()
          Returns the class that is used for values.
 void install(JFormattedTextField ftf)
          Installs the formatter on the specified JFormattedTextField.
 void setAllowsInvalid(boolean allowsInvalid)
          Sets the value of the allowsInvalid property.
 void setCommitsOnValidEdit(boolean commitsOnValidEdit)
          Sets the value of the commitsOnValidEdit property.
 void setOverwriteMode(boolean overwriteMode)
          Sets the value of the overwriteMode property.
 void setValueClass(Class<?> valueClass)
          Sets the class that is used for values.
 Object stringToValue(String string)
          Converts a String (from the JFormattedTextField input) to a value.
 String valueToString(Object value)
          Converts a value object into a String.
 
Methods inherited from class javax.swing.JFormattedTextField.AbstractFormatter
getActions, getFormattedTextField, getNavigationFilter, invalidEdit, setEditValid, uninstall
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultFormatter

public DefaultFormatter()
Creates a new instance of DefaultFormatter.

Method Detail

install

public void install(JFormattedTextField ftf)
Installs the formatter on the specified 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.

Overrides:
install in class JFormattedTextField.AbstractFormatter
Parameters:
ftf - the JFormattedTextField in which this formatter is installed

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:
setCommitsOnValidEdit(boolean)

setCommitsOnValidEdit

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

Parameters:
commitsOnValidEdit - the new state of the commitsOnValidEdit property
See Also:
getCommitsOnValidEdit()

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

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

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

setAllowsInvalid

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

Parameters:
allowsInvalid - the new value for the property
See Also:
getAllowsInvalid()

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

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

Specified by:
stringToValue in class JFormattedTextField.AbstractFormatter
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)
                     throws ParseException
Converts a value object into a String. This is done by invoking the Object.toString() method on the value.

Specified by:
valueToString in class JFormattedTextField.AbstractFormatter
Parameters:
value - the value to be converted
Returns:
the string representation of the value
Throws:
ParseException - if the value cannot be converted

clone

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

Overrides:
clone in class JFormattedTextField.AbstractFormatter
Returns:
a clone of this object
Throws:
CloneNotSupportedException - not thrown here
See Also:
Cloneable

getDocumentFilter

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

Overrides:
getDocumentFilter in class JFormattedTextField.AbstractFormatter
Returns:
the DocumentFilter that is used to restrict input