javax.swing.text

Class DefaultFormatter

Implemented Interfaces:
Cloneable, Serializable
Known Direct 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

clone, getActions, getDocumentFilter, getFormattedTextField, getNavigationFilter, install, invalidEdit, setEditValid, stringToValue, uninstall, valueToString

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

DefaultFormatter

public DefaultFormatter()
Creates a new instance of DefaultFormatter.

Method Details

clone

public Object clone()
            throws CloneNotSupportedException
Creates and returns a clone of this DefaultFormatter.
Overrides:
clone in interface JFormattedTextField.AbstractFormatter
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

getDocumentFilter

protected DocumentFilter getDocumentFilter()
Returns the DocumentFilter that is used to restrict input.
Overrides:
getDocumentFilter in interface JFormattedTextField.AbstractFormatter
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 JFormattedTextField. This method does the following things:
  • Display the value of #valueToString in the JFormattedTextField
  • Install the Actions from #getActions on the JTextField
  • Install the DocumentFilter returned by #getDocumentFilter
  • Install the NavigationFilter returned by #getNavigationFilter
This method is typically not overridden by subclasses. Instead override one of the mentioned methods in order to customize behaviour.
Overrides:
install in interface JFormattedTextField.AbstractFormatter
Parameters:
ftf - the 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

setCommitsOnValidEdit

public void setCommitsOnValidEdit(boolean commitsOnValidEdit)
Sets the value of the commitsOnValidEdit property.
Parameters:
commitsOnValidEdit - the new state of the commitsOnValidEdit 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

setValueClass

public void setValueClass(Class valueClass)
Sets the class that is used for values.
Parameters:
valueClass - the class that is used for values

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.
Overrides:
stringToValue in interface 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.
Overrides:
valueToString in interface JFormattedTextField.AbstractFormatter
Parameters:
value - the value to be converted
Returns:
the string representation of the value
Throws:
ParseException - if the value cannot be converted

DefaultFormatter.java -- Copyright (C) 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.