- java.lang.Object
- 
- 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 ofAbstractFormatterare used byJFormattedTextFieldto 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 theJFormattedTextFieldin any way it sees fit to enforce the desired policy.An AbstractFormattercan only be active in oneJFormattedTextFieldat a time.JFormattedTextFieldinvokesinstallwhen it is ready to use it followed byuninstallwhen done. Subclasses that wish to install additional state should overrideinstalland message super appropriately.Subclasses must override the conversion methods stringToValueandvalueToString. Optionally they can overridegetActions,getNavigationFilterandgetDocumentFilterto restrict theJFormattedTextFieldin a particular way.Subclasses that allow the JFormattedTextFieldto be in a temporarily invalid state should invokesetEditValidat the appropriate times.- Since:
- 1.4
- See Also:
- Serialized Form
 
- 
- 
Constructor SummaryConstructors Constructor Description AbstractFormatter()
 - 
Method SummaryAll Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected Objectclone()Clones theAbstractFormatter.protected Action[]getActions()Subclass and override if you wish to provide a custom set ofActions.protected DocumentFiltergetDocumentFilter()Subclass and override if you wish to provide aDocumentFilterto restrict what can be input.protected JFormattedTextFieldgetFormattedTextField()Returns the currentJFormattedTextFieldtheAbstractFormatteris installed on.protected NavigationFiltergetNavigationFilter()Subclass and override if you wish to provide a filter to restrict where the user can navigate to.voidinstall(JFormattedTextField ftf)Installs theAbstractFormatteronto a particularJFormattedTextField.protected voidinvalidEdit()This 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.voiduninstall()Uninstalls any state theAbstractFormattermay have installed on theJFormattedTextField.abstract StringvalueToString(Object value)Returns the string value to display forvalue.
 
- 
- 
- 
Method Detail- 
installpublic void install(JFormattedTextField ftf) 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.
 
 - 
uninstallpublic void uninstall() Uninstalls any state theAbstractFormattermay have installed on theJFormattedTextField. This resets theDocumentFilter,NavigationFilterand additionalActions installed on theJFormattedTextField.
 - 
stringToValuepublic abstract Object stringToValue(String text) throws ParseException 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
 
 - 
valueToStringpublic abstract String valueToString(Object value) throws ParseException 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
 
 - 
getFormattedTextFieldprotected JFormattedTextField getFormattedTextField() Returns the currentJFormattedTextFieldtheAbstractFormatteris installed on.- Returns:
- JFormattedTextField formatting for.
 
 - 
invalidEditprotected void invalidEdit() This should be invoked when the user types an invalid character. This forwards the call to the current JFormattedTextField.
 - 
setEditValidprotected 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
 
 - 
getActionsprotected Action[] 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
 
 - 
getDocumentFilterprotected DocumentFilter getDocumentFilter() Subclass and override if you wish to provide aDocumentFilterto restrict what can be input.installwill install the returned value onto theJFormattedTextField.- Returns:
- DocumentFilter to restrict edits
 
 - 
getNavigationFilterprotected NavigationFilter getNavigationFilter() Subclass and override if you wish to provide a filter to restrict where the user can navigate to.installwill install the returned value onto theJFormattedTextField.- Returns:
- NavigationFilter to restrict navigation
 
 - 
cloneprotected Object clone() throws CloneNotSupportedException Clones theAbstractFormatter. The returned instance is not associated with aJFormattedTextField.- Overrides:
- clonein class- Object
- Returns:
- Copy of the AbstractFormatter
- Throws:
- CloneNotSupportedException- if the object's class does not support the- Cloneableinterface. Subclasses that override the- clonemethod can also throw this exception to indicate that an instance cannot be cloned.
- See Also:
- Cloneable
 
 
- 
 
-