JFormattedTextField.AbstractFormatter (Java Platform SE 8 ) (original) (raw)
- javax.swing.JFormattedTextField.AbstractFormatter
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
DefaultFormatter
Enclosing class:
JFormattedTextField
public abstract static class JFormattedTextField.AbstractFormatter
extends Object
implements Serializable
Instances of AbstractFormatter
are used byJFormattedTextField
to handle the conversion both from an Object to a String, and back from a String to an Object.AbstractFormatter
s can also enforce editing policies, or navigation policies, or manipulate theJFormattedTextField
in any way it sees fit to enforce the desired policy.
An AbstractFormatter
can only be active in one JFormattedTextField
at a time.JFormattedTextField
invokesinstall
when it is ready to use it followed by uninstall
when done. Subclasses that wish to install additional state should overrideinstall
and message super appropriately.
Subclasses must override the conversion methodsstringToValue
and valueToString
. Optionally they can override getActions
,getNavigationFilter
and getDocumentFilter
to restrict the JFormattedTextField
in a particular way.
Subclasses that allow the JFormattedTextField
to be in a temporarily invalid state should invoke setEditValid
at the appropriate times.
Since:
1.4
Constructor Summary
Constructors
Constructor Description AbstractFormatter() Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods
Modifier and Type Method Description protected Object clone() Clones the AbstractFormatter. protected Action[] getActions() Subclass and override if you wish to provide a custom set ofActions. protected DocumentFilter getDocumentFilter() Subclass and override if you wish to provide aDocumentFilter to restrict what can be input. protected JFormattedTextField getFormattedTextField() Returns the current JFormattedTextField theAbstractFormatter is installed on. protected NavigationFilter getNavigationFilter() Subclass and override if you wish to provide a filter to restrict where the user can navigate to. void install(JFormattedTextField ftf) Installs the AbstractFormatter onto a particularJFormattedTextField. protected void invalidEdit() This should be invoked when the user types an invalid character. protected void setEditValid(boolean valid) Invoke this to update the editValid property of theJFormattedTextField. abstract Object stringToValue(String text) Parses text returning an arbitrary Object. void uninstall() Uninstalls any state the AbstractFormatter may have installed on the JFormattedTextField. abstract String valueToString(Object value) Returns the string value to display for value. * ### 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
* #### AbstractFormatter public AbstractFormatter()
Method Detail
* #### install public void install([JFormattedTextField](../../javax/swing/JFormattedTextField.html "class in javax.swing") ftf) Installs the `AbstractFormatter` 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. You will only need to invoke this yourself if you are subclassing `JFormattedTextField` and installing/uninstalling `AbstractFormatter` at a different time than `JFormattedTextField` does. Parameters: `ftf` \- JFormattedTextField to format for, may be null indicating uninstall from current JFormattedTextField. * #### uninstall public void uninstall() Uninstalls any state the `AbstractFormatter` may have installed on the `JFormattedTextField`. This resets the`DocumentFilter`, `NavigationFilter` and additional `Action`s installed on the`JFormattedTextField`. * #### stringToValue public abstract [Object](../../java/lang/Object.html "class in java.lang") stringToValue([String](../../java/lang/String.html "class in java.lang") text) throws [ParseException](../../java/text/ParseException.html "class in java.text") Parses `text` returning an arbitrary Object. Some formatters may return null. Parameters: `text` \- 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 abstract [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") Returns the string value to display for `value`. 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 * #### getFormattedTextField protected [JFormattedTextField](../../javax/swing/JFormattedTextField.html "class in javax.swing") getFormattedTextField() Returns the current `JFormattedTextField` the`AbstractFormatter` is installed on. Returns: JFormattedTextField formatting for. * #### invalidEdit protected void invalidEdit() This should be invoked when the user types an invalid character. This forwards the call to the current JFormattedTextField. * #### setEditValid protected void setEditValid(boolean valid) Invoke this to update the `editValid` property of the`JFormattedTextField`. If you an enforce a policy such that the `JFormattedTextField` is always in a valid state, you will never need to invoke this. Parameters: `valid` \- Valid state of the JFormattedTextField * #### getActions protected [Action](../../javax/swing/Action.html "interface in javax.swing")[] getActions() Subclass and override if you wish to provide a custom set of`Action`s. `install` will install these on the `JFormattedTextField`'s `ActionMap`. Returns: Array of Actions to install on JFormattedTextField * #### getDocumentFilter protected [DocumentFilter](../../javax/swing/text/DocumentFilter.html "class in javax.swing.text") getDocumentFilter() Subclass and override if you wish to provide a`DocumentFilter` to restrict what can be input.`install` will install the returned value onto the `JFormattedTextField`. Returns: DocumentFilter to restrict edits * #### getNavigationFilter protected [NavigationFilter](../../javax/swing/text/NavigationFilter.html "class in javax.swing.text") getNavigationFilter() Subclass and override if you wish to provide a filter to restrict where the user can navigate to.`install` will install the returned value onto the `JFormattedTextField`. Returns: NavigationFilter to restrict navigation * #### clone protected [Object](../../java/lang/Object.html "class in java.lang") clone() throws [CloneNotSupportedException](../../java/lang/CloneNotSupportedException.html "class in java.lang") Clones the `AbstractFormatter`. The returned instance is not associated with a `JFormattedTextField`. Overrides: `[clone](../../java/lang/Object.html#clone--)` in class `[Object](../../java/lang/Object.html "class in java.lang")` Returns: Copy of the AbstractFormatter 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.