DefaultFormatter (Java Platform SE 8 ) (original) (raw)
- 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, SerializableDefaultFormatter
formats arbitrary objects. Formatting is done by invoking the toString
method. 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 DefaultFormatter
can not be used in multiple instances of JFormattedTextField
. To obtain a copy of an already configured DefaultFormatter
, use theclone
method.
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.beans
package. Please see XMLEncoder.
Since:
1.4
See Also:
JFormattedTextField.AbstractFormatter
Constructor Summary
Constructors
Constructor Description DefaultFormatter() Creates a DefaultFormatter. Method Summary
All Methods Instance Methods Concrete Methods
Modifier and Type Method Description Object clone() Creates a copy of the DefaultFormatter. boolean getAllowsInvalid() Returns whether or not the value being edited is allowed to be invalid for a length of time. boolean getCommitsOnValidEdit() Returns when edits are published back to theJFormattedTextField. protected DocumentFilter getDocumentFilter() Returns the DocumentFilter used to restrict the characters that can be input into the JFormattedTextField. protected NavigationFilter getNavigationFilter() Returns the NavigationFilter used to restrict where the cursor can be placed. boolean getOverwriteMode() Returns the behavior when inserting characters. Class<?> getValueClass() Returns that class that is used to create new Objects. void install(JFormattedTextField ftf) Installs the DefaultFormatter onto a particularJFormattedTextField. void setAllowsInvalid(boolean allowsInvalid) Sets whether or not the value being edited is allowed to be invalid for a length of time (that is, stringToValue throws a ParseException). void setCommitsOnValidEdit(boolean commit) Sets when edits are published back to theJFormattedTextField. void setOverwriteMode(boolean overwriteMode) Configures the behavior when inserting characters. void setValueClass(Class<?> valueClass) Sets that class that is used to create new Objects. Object stringToValue(String string) Converts the passed in String into an instance ofgetValueClass by way of the constructor that takes a String argument. String valueToString(Object value) Converts the passed in Object into a String by way of thetoString method. * ### Methods inherited from class javax.swing.[JFormattedTextField.AbstractFormatter](../../../javax/swing/JFormattedTextField.AbstractFormatter.html "class in javax.swing") `[getActions](../../../javax/swing/JFormattedTextField.AbstractFormatter.html#getActions--), [getFormattedTextField](../../../javax/swing/JFormattedTextField.AbstractFormatter.html#getFormattedTextField--), [invalidEdit](../../../javax/swing/JFormattedTextField.AbstractFormatter.html#invalidEdit--), [setEditValid](../../../javax/swing/JFormattedTextField.AbstractFormatter.html#setEditValid-boolean-), [uninstall](../../../javax/swing/JFormattedTextField.AbstractFormatter.html#uninstall--)` * ### Methods inherited from class java.lang.[Object](../../../java/lang/Object.html "class in java.lang") `[equals](../../../java/lang/Object.html#equals-java.lang.Object-), [finalize](../../../java/lang/Object.html#finalize--), [getClass](../../../java/lang/Object.html#getClass--), [hashCode](../../../java/lang/Object.html#hashCode--), [notify](../../../java/lang/Object.html#notify--), [notifyAll](../../../java/lang/Object.html#notifyAll--), [toString](../../../java/lang/Object.html#toString--), [wait](../../../java/lang/Object.html#wait--), [wait](../../../java/lang/Object.html#wait-long-), [wait](../../../java/lang/Object.html#wait-long-int-)`
Constructor Detail
* #### DefaultFormatter public DefaultFormatter() Creates a DefaultFormatter.
Method Detail
* #### install public void install([JFormattedTextField](../../../javax/swing/JFormattedTextField.html "class in javax.swing") ftf) Installs the `DefaultFormatter` onto a particular`JFormattedTextField`. This will invoke `valueToString` to convert the current value from the `JFormattedTextField` to a String. This will then install the `Action`s from`getActions`, the `DocumentFilter` returned from `getDocumentFilter` and the`NavigationFilter` returned from`getNavigationFilter` onto the`JFormattedTextField`. Subclasses will typically only need to override this if they wish to install additional listeners on the`JFormattedTextField`. If there is a `ParseException` in converting the current value to a String, this will set the text to an empty String, and mark the `JFormattedTextField` as being in an invalid state. While this is a public method, this is typically only useful for subclassers of `JFormattedTextField`.`JFormattedTextField` will invoke this method at the appropriate times when the value changes, or its internal state changes. Overrides: `[install](../../../javax/swing/JFormattedTextField.AbstractFormatter.html#install-javax.swing.JFormattedTextField-)` in class `[JFormattedTextField.AbstractFormatter](../../../javax/swing/JFormattedTextField.AbstractFormatter.html "class in javax.swing")` 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 the`JFormattedTextField`. If true, `commitEdit` is invoked after every valid edit (any time the text is edited). On the other hand, if this is false than the `DefaultFormatter` does not publish edits back to the `JFormattedTextField`. As such, the only time the value of the `JFormattedTextField` will change is when `commitEdit` is invoked on`JFormattedTextField`, typically when enter is pressed or focus leaves the `JFormattedTextField`. Parameters: `commit` \- Used to indicate when edits are committed back to the JTextComponent * #### getCommitsOnValidEdit public boolean getCommitsOnValidEdit() Returns when edits are published back to the`JFormattedTextField`. Returns: true if edits are committed after every valid edit * #### setOverwriteMode public void setOverwriteMode(boolean overwriteMode) Configures the behavior when inserting characters. If`overwriteMode` is 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, `stringToValue` throws a `ParseException`). 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](../../../java/lang/Class.html "class in java.lang")<?> 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](../../../java/lang/Class.html "class in java.lang")<?> getValueClass() Returns that class that is used to create new Objects. Returns: Class used to construct return value from stringToValue * #### stringToValue public [Object](../../../java/lang/Object.html "class in java.lang") stringToValue([String](../../../java/lang/String.html "class in java.lang") string) throws [ParseException](../../../java/text/ParseException.html "class in java.text") Converts the passed in String into an instance of`getValueClass` by way of the constructor that takes a String argument. If `getValueClass` returns null, the Class of the current value in the`JFormattedTextField` will be used. If this is null, a String will be returned. If the constructor throws an exception, a`ParseException` will be thrown. If there is no single argument String constructor, `string` will be returned. Specified by: `[stringToValue](../../../javax/swing/JFormattedTextField.AbstractFormatter.html#stringToValue-java.lang.String-)` in class `[JFormattedTextField.AbstractFormatter](../../../javax/swing/JFormattedTextField.AbstractFormatter.html "class in javax.swing")` Parameters: `string` \- String to convert Returns: Object representation of text Throws: `[ParseException](../../../java/text/ParseException.html "class in java.text")` \- if there is an error in the conversion * #### valueToString public [String](../../../java/lang/String.html "class in java.lang") valueToString([Object](../../../java/lang/Object.html "class in java.lang") value) throws [ParseException](../../../java/text/ParseException.html "class in java.text") Converts the passed in Object into a String by way of the`toString` method. Specified by: `[valueToString](../../../javax/swing/JFormattedTextField.AbstractFormatter.html#valueToString-java.lang.Object-)` in class `[JFormattedTextField.AbstractFormatter](../../../javax/swing/JFormattedTextField.AbstractFormatter.html "class in javax.swing")` Parameters: `value` \- Value to convert Returns: String representation of value Throws: `[ParseException](../../../java/text/ParseException.html "class in java.text")` \- if there is an error in the conversion * #### getDocumentFilter protected [DocumentFilter](../../../javax/swing/text/DocumentFilter.html "class in javax.swing.text") getDocumentFilter() Returns the `DocumentFilter` used to restrict the characters that can be input into the `JFormattedTextField`. Overrides: `[getDocumentFilter](../../../javax/swing/JFormattedTextField.AbstractFormatter.html#getDocumentFilter--)` in class `[JFormattedTextField.AbstractFormatter](../../../javax/swing/JFormattedTextField.AbstractFormatter.html "class in javax.swing")` Returns: DocumentFilter to restrict edits * #### getNavigationFilter protected [NavigationFilter](../../../javax/swing/text/NavigationFilter.html "class in javax.swing.text") getNavigationFilter() Returns the `NavigationFilter` used to restrict where the cursor can be placed. Overrides: `[getNavigationFilter](../../../javax/swing/JFormattedTextField.AbstractFormatter.html#getNavigationFilter--)` in class `[JFormattedTextField.AbstractFormatter](../../../javax/swing/JFormattedTextField.AbstractFormatter.html "class in javax.swing")` Returns: NavigationFilter to restrict navigation * #### clone public [Object](../../../java/lang/Object.html "class in java.lang") clone() throws [CloneNotSupportedException](../../../java/lang/CloneNotSupportedException.html "class in java.lang") Creates a copy of the DefaultFormatter. Overrides: `[clone](../../../javax/swing/JFormattedTextField.AbstractFormatter.html#clone--)` in class `[JFormattedTextField.AbstractFormatter](../../../javax/swing/JFormattedTextField.AbstractFormatter.html "class in javax.swing")` Returns: copy of the DefaultFormatter Throws: `[CloneNotSupportedException](../../../java/lang/CloneNotSupportedException.html "class in java.lang")` \- if the object's class does not support the `Cloneable` interface. Subclasses that override the `clone` method can also throw this exception to indicate that an instance cannot be cloned. See Also: [Cloneable](../../../java/lang/Cloneable.html "interface in java.lang")
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2025, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.