- java.lang.Object
-
- javax.swing.JFormattedTextField.AbstractFormatter
-
- javax.swing.text.DefaultFormatter
-
- All Implemented Interfaces:
Serializable,Cloneable
- Direct Known Subclasses:
InternationalFormatter,MaskFormatter
public class DefaultFormatter extends JFormattedTextField.AbstractFormatter implements Cloneable, Serializable
DefaultFormatterformats arbitrary objects. Formatting is done by invoking thetoStringmethod. In order to convert the value back to a String, your class must provide a constructor that takes a String argument. If no single argument constructor that takes a String is found, the returned value will be the String passed intostringToValue.Instances of
DefaultFormattercan not be used in multiple instances ofJFormattedTextField. To obtain a copy of an already configuredDefaultFormatter, use theclonemethod.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.beanspackage. Please seeXMLEncoder.- Since:
- 1.4
- See Also:
JFormattedTextField.AbstractFormatter, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description DefaultFormatter()Creates a DefaultFormatter.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Objectclone()Creates a copy of the DefaultFormatter.booleangetAllowsInvalid()Returns whether or not the value being edited is allowed to be invalid for a length of time.booleangetCommitsOnValidEdit()Returns when edits are published back to theJFormattedTextField.protected DocumentFiltergetDocumentFilter()Returns theDocumentFilterused to restrict the characters that can be input into theJFormattedTextField.protected NavigationFiltergetNavigationFilter()Returns theNavigationFilterused to restrict where the cursor can be placed.booleangetOverwriteMode()Returns the behavior when inserting characters.Class<?>getValueClass()Returns that class that is used to create new Objects.voidinstall(JFormattedTextField ftf)Installs theDefaultFormatteronto a particularJFormattedTextField.voidsetAllowsInvalid(boolean allowsInvalid)Sets whether or not the value being edited is allowed to be invalid for a length of time (that is,stringToValuethrows aParseException).voidsetCommitsOnValidEdit(boolean commit)Sets when edits are published back to theJFormattedTextField.voidsetOverwriteMode(boolean overwriteMode)Configures the behavior when inserting characters.voidsetValueClass(Class<?> valueClass)Sets that class that is used to create new Objects.ObjectstringToValue(String string)Converts the passed in String into an instance ofgetValueClassby way of the constructor that takes a String argument.StringvalueToString(Object value)Converts the passed in Object into a String by way of thetoStringmethod.-
Methods declared in class javax.swing.JFormattedTextField.AbstractFormatter
getActions, getFormattedTextField, invalidEdit, setEditValid, uninstall
-
-
-
-
Method Detail
-
install
public void install(JFormattedTextField ftf)
Installs theDefaultFormatteronto a particularJFormattedTextField. This will invokevalueToStringto convert the current value from theJFormattedTextFieldto a String. This will then install theActions fromgetActions, theDocumentFilterreturned fromgetDocumentFilterand theNavigationFilterreturned fromgetNavigationFilteronto theJFormattedTextField.Subclasses will typically only need to override this if they wish to install additional listeners on the
JFormattedTextField.If there is a
ParseExceptionin converting the current value to a String, this will set the text to an empty String, and mark theJFormattedTextFieldas being in an invalid state.While this is a public method, this is typically only useful for subclassers of
JFormattedTextField.JFormattedTextFieldwill invoke this method at the appropriate times when the value changes, or its internal state changes.- Overrides:
installin classJFormattedTextField.AbstractFormatter- Parameters:
ftf- JFormattedTextField to format for, may be null indicating uninstall from current JFormattedTextField.
-
setCommitsOnValidEdit
public void setCommitsOnValidEdit(boolean commit)
Sets when edits are published back to theJFormattedTextField. If true,commitEditis invoked after every valid edit (any time the text is edited). On the other hand, if this is false than theDefaultFormatterdoes not publish edits back to theJFormattedTextField. As such, the only time the value of theJFormattedTextFieldwill change is whencommitEditis invoked onJFormattedTextField, typically when enter is pressed or focus leaves theJFormattedTextField.- Parameters:
commit- Used to indicate when edits are committed back to the JTextComponent
-
getCommitsOnValidEdit
public boolean getCommitsOnValidEdit()
Returns when edits are published back to theJFormattedTextField.- Returns:
- true if edits are committed after every valid edit
-
setOverwriteMode
public void setOverwriteMode(boolean overwriteMode)
Configures the behavior when inserting characters. IfoverwriteModeis true (the default), new characters overwrite existing characters in the model.- Parameters:
overwriteMode- Indicates if overwrite or overstrike mode is used
-
getOverwriteMode
public boolean getOverwriteMode()
Returns the behavior when inserting characters.- Returns:
- true if newly inserted characters overwrite existing characters
-
setAllowsInvalid
public void setAllowsInvalid(boolean allowsInvalid)
Sets whether or not the value being edited is allowed to be invalid for a length of time (that is,stringToValuethrows aParseException). It is often convenient to allow the user to temporarily input an invalid value.- Parameters:
allowsInvalid- Used to indicate if the edited value must always be valid
-
getAllowsInvalid
public boolean getAllowsInvalid()
Returns whether or not the value being edited is allowed to be invalid for a length of time.- Returns:
- false if the edited value must always be valid
-
setValueClass
public void setValueClass(Class<?> valueClass)
Sets that class that is used to create new Objects. If the passed in class does not have a single argument constructor that takes a String, String values will be used.- Parameters:
valueClass- Class used to construct return value from stringToValue
-
getValueClass
public Class<?> getValueClass()
Returns that class that is used to create new Objects.- Returns:
- Class used to construct return value from stringToValue
-
stringToValue
public Object stringToValue(String string) throws ParseException
Converts the passed in String into an instance ofgetValueClassby way of the constructor that takes a String argument. IfgetValueClassreturns null, the Class of the current value in theJFormattedTextFieldwill be used. If this is null, a String will be returned. If the constructor throws an exception, aParseExceptionwill be thrown. If there is no single argument String constructor,stringwill be returned.- Specified by:
stringToValuein classJFormattedTextField.AbstractFormatter- Parameters:
string- String to convert- Returns:
- Object representation of text
- Throws:
ParseException- if there is an error in the conversion
-
valueToString
public String valueToString(Object value) throws ParseException
Converts the passed in Object into a String by way of thetoStringmethod.- Specified by:
valueToStringin classJFormattedTextField.AbstractFormatter- Parameters:
value- Value to convert- Returns:
- String representation of value
- Throws:
ParseException- if there is an error in the conversion
-
getDocumentFilter
protected DocumentFilter getDocumentFilter()
Returns theDocumentFilterused to restrict the characters that can be input into theJFormattedTextField.- Overrides:
getDocumentFilterin classJFormattedTextField.AbstractFormatter- Returns:
- DocumentFilter to restrict edits
-
getNavigationFilter
protected NavigationFilter getNavigationFilter()
Returns theNavigationFilterused to restrict where the cursor can be placed.- Overrides:
getNavigationFilterin classJFormattedTextField.AbstractFormatter- Returns:
- NavigationFilter to restrict navigation
-
clone
public Object clone() throws CloneNotSupportedException
Creates a copy of the DefaultFormatter.- Overrides:
clonein classJFormattedTextField.AbstractFormatter- Returns:
- copy of the DefaultFormatter
- Throws:
CloneNotSupportedException- if the receiver's class does not implement the interface Cloneable.
-
-