Class JFormattedTextField.AbstractFormatter
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
DefaultFormatter
- Enclosing class:
JFormattedTextField
AbstractFormatter are used by
JFormattedTextField to handle the conversion both
from an Object to a String, and back from a String to an Object.
AbstractFormatters can also enforce editing policies,
or navigation policies, or manipulate the
JFormattedTextField in any way it sees fit to
enforce the desired policy.
An AbstractFormatter can only be active in
one JFormattedTextField at a time.
JFormattedTextField invokes
install when it is ready to use it followed
by uninstall when done. Subclasses
that wish to install additional state should override
install and message super appropriately.
Subclasses must override the conversion methods
stringToValue 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 -
Method Summary
Modifier and TypeMethodDescriptionprotected Objectclone()Clones theAbstractFormatter.protected Action[]Subclass and override if you wish to provide a custom set ofActions.protected DocumentFilterSubclass and override if you wish to provide aDocumentFilterto restrict what can be input.protected JFormattedTextFieldReturns the currentJFormattedTextFieldtheAbstractFormatteris installed on.protected NavigationFilterSubclass and override if you wish to provide a filter to restrict where the user can navigate to.voidInstalls theAbstractFormatteronto a particularJFormattedTextField.protected voidThis should be invoked when the user types an invalid character.protected voidsetEditValid(boolean valid) Invoke this to update theeditValidproperty of theJFormattedTextField.abstract ObjectstringToValue(String text) Parsestextreturning an arbitrary Object.voidUninstalls any state theAbstractFormattermay have installed on theJFormattedTextField.abstract StringvalueToString(Object value) Returns the string value to display forvalue.Methods declared in class Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitModifier and TypeMethodDescriptionbooleanCompares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.May cause performance issues, deadlocks and hangs.getClass()Answers the unique instance of java.lang.Class which represents the class of the receiver.inthashCode()Answers an integer hash code for the receiver.final voidnotify()Causes one thread which iswaiting on the receiver to be made ready to run.final voidCauses all threads which arewaiting on the receiver to be made ready to run.toString()Answers a string containing a concise, human-readable description of the receiver.final voidwait()Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated bynotifyornotifyAll).final voidwait(long time) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.final voidwait(long time, int frac) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.
-
Constructor Details
-
AbstractFormatter
protected AbstractFormatter()Constructor for subclasses to call.
-
-
Method Details
-
install
Installs theAbstractFormatteronto 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. You will only need to invoke this yourself if you are subclassingJFormattedTextFieldand installing/uninstallingAbstractFormatterat a different time thanJFormattedTextFielddoes.- Parameters:
ftf- JFormattedTextField to format for, may be null indicating uninstall from current JFormattedTextField.
-
uninstall
public void uninstall()Uninstalls any state theAbstractFormattermay have installed on theJFormattedTextField. This resets theDocumentFilter,NavigationFilterand additionalActions installed on theJFormattedTextField. -
stringToValue
Parsestextreturning an arbitrary Object. Some formatters may return null.- Parameters:
text- String to convert- Returns:
- Object representation of text
- Throws:
ParseException- if there is an error in the conversion
-
valueToString
Returns the string value to display forvalue.- Parameters:
value- Value to convert- Returns:
- String representation of value
- Throws:
ParseException- if there is an error in the conversion
-
getFormattedTextField
Returns the currentJFormattedTextFieldtheAbstractFormatteris 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 theeditValidproperty of theJFormattedTextField. If you an enforce a policy such that theJFormattedTextFieldis always in a valid state, you will never need to invoke this.- Parameters:
valid- Valid state of the JFormattedTextField
-
getActions
Subclass and override if you wish to provide a custom set ofActions.installwill install these on theJFormattedTextField'sActionMap.- Returns:
- Array of Actions to install on JFormattedTextField
-
getDocumentFilter
Subclass and override if you wish to provide aDocumentFilterto restrict what can be input.installwill install the returned value onto theJFormattedTextField.- Implementation Requirements:
- The default implementation returns
null. - Returns:
- DocumentFilter to restrict edits
-
clone
Clones theAbstractFormatter. The returned instance is not associated with aJFormattedTextField.- Overrides:
clonein classObject- Returns:
- Copy of the AbstractFormatter
- Throws:
CloneNotSupportedException- if the receiver's class does not implement the interface Cloneable.
-