- java.lang.Object
-
- javax.swing.JFormattedTextField.AbstractFormatter
-
- javax.swing.text.DefaultFormatter
-
- javax.swing.text.InternationalFormatter
-
- All Implemented Interfaces:
Serializable,Cloneable
- Direct Known Subclasses:
DateFormatter,NumberFormatter
public class InternationalFormatter extends DefaultFormatter
InternationalFormatterextendsDefaultFormatter, using an instance ofjava.text.Formatto handle the conversion to a String, and the conversion from a String.If
getAllowsInvalid()is false, this will ask theFormatto format the current text on every edit.You can specify a minimum and maximum value by way of the
setMinimumandsetMaximummethods. In order for this to work the values returned fromstringToValuemust be comparable to the min/max values by way of theComparableinterface.Be careful how you configure the
Formatand theInternationalFormatter, as it is possible to create a situation where certain values can not be input. Consider the date format 'M/d/yy', anInternationalFormatterthat is always valid (setAllowsInvalid(false)), is in overwrite mode (setOverwriteMode(true)) and the date 7/1/99. In this case the user will not be able to enter a two digit month or day of month. To avoid this, the format should be 'MM/dd/yy'.If
InternationalFormatteris configured to only allow valid values (setAllowsInvalid(false)), every valid edit will result in the text of theJFormattedTextFieldbeing completely reset from theFormat. The cursor position will also be adjusted as literal characters are added/removed from the resulting String.InternationalFormatter's behavior ofstringToValueis slightly different than that ofDefaultTextFormatter, it does the following:parseObjectis invoked on theFormatspecified bysetFormat- If a Class has been set for the values (
setValueClass), supers implementation is invoked to convert the value returned fromparseObjectto the appropriate class. - If a
ParseExceptionhas not been thrown, and the value is outside the min/max aParseExceptionis thrown. - The value is returned.
InternationalFormatterimplementsstringToValuein this manner so that you can specify an alternate Class thanFormatmay return.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:
Format,Comparable, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description InternationalFormatter()Creates anInternationalFormatterwith noFormatspecified.InternationalFormatter(Format format)Creates anInternationalFormatterwith the specifiedFormatinstance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Objectclone()Creates a copy of the DefaultFormatter.protected Action[]getActions()IfgetSupportsIncrementreturns true, this returns two Actions suitable for incrementing/decrementing the value.Format.Field[]getFields(int offset)Returns theFormat.Fieldconstants associated with the text atoffset.FormatgetFormat()Returns the format that dictates the legal values that can be edited and displayed.Comparable<?>getMaximum()Returns the maximum permissible value.Comparable<?>getMinimum()Returns the minimum permissible value.voidinstall(JFormattedTextField ftf)Installs theDefaultFormatteronto a particularJFormattedTextField.voidsetFormat(Format format)Sets the format that dictates the legal values that can be edited and displayed.voidsetMaximum(Comparable<?> max)Sets the maximum permissible value.voidsetMinimum(Comparable<?> minimum)Sets the minimum permissible value.ObjectstringToValue(String text)Returns theObjectrepresentation of theStringtext.StringvalueToString(Object value)Returns a String representation of the Objectvalue.-
Methods declared in class javax.swing.text.DefaultFormatter
getAllowsInvalid, getCommitsOnValidEdit, getDocumentFilter, getNavigationFilter, getOverwriteMode, getValueClass, setAllowsInvalid, setCommitsOnValidEdit, setOverwriteMode, setValueClass
-
Methods declared in class javax.swing.JFormattedTextField.AbstractFormatter
getFormattedTextField, invalidEdit, setEditValid, uninstall
-
-
-
-
Constructor Detail
-
InternationalFormatter
public InternationalFormatter()
Creates anInternationalFormatterwith noFormatspecified.
-
InternationalFormatter
public InternationalFormatter(Format format)
Creates anInternationalFormatterwith the specifiedFormatinstance.- Parameters:
format- Format instance used for converting from/to Strings
-
-
Method Detail
-
setFormat
public void setFormat(Format format)
Sets the format that dictates the legal values that can be edited and displayed.- Parameters:
format-Formatinstance used for converting from/to Strings
-
getFormat
public Format getFormat()
Returns the format that dictates the legal values that can be edited and displayed.- Returns:
- Format instance used for converting from/to Strings
-
setMinimum
public void setMinimum(Comparable<?> minimum)
Sets the minimum permissible value. If thevalueClasshas not been specified, andminimumis non null, thevalueClasswill be set to that of the class ofminimum.- Parameters:
minimum- Minimum legal value that can be input- See Also:
DefaultFormatter.setValueClass(java.lang.Class<?>)
-
getMinimum
public Comparable<?> getMinimum()
Returns the minimum permissible value.- Returns:
- Minimum legal value that can be input
-
setMaximum
public void setMaximum(Comparable<?> max)
Sets the maximum permissible value. If thevalueClasshas not been specified, andmaxis non null, thevalueClasswill be set to that of the class ofmax.- Parameters:
max- Maximum legal value that can be input- See Also:
DefaultFormatter.setValueClass(java.lang.Class<?>)
-
getMaximum
public Comparable<?> getMaximum()
Returns the maximum permissible value.- Returns:
- Maximum legal value that can be input
-
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 classDefaultFormatter- Parameters:
ftf- JFormattedTextField to format for, may be null indicating uninstall from current JFormattedTextField.
-
valueToString
public String valueToString(Object value) throws ParseException
Returns a String representation of the Objectvalue. This invokesformaton the currentFormat.- Overrides:
valueToStringin classDefaultFormatter- Parameters:
value- Value to convert- Returns:
- String representation of value
- Throws:
ParseException- if there is an error in the conversion
-
stringToValue
public Object stringToValue(String text) throws ParseException
Returns theObjectrepresentation of theStringtext.- Overrides:
stringToValuein classDefaultFormatter- Parameters:
text-Stringto convert- Returns:
Objectrepresentation of text- Throws:
ParseException- if there is an error in the conversion
-
getFields
public Format.Field[] getFields(int offset)
Returns theFormat.Fieldconstants associated with the text atoffset. Ifoffsetis not a valid location into the current text, this will return an empty array.- Parameters:
offset- offset into text to be examined- Returns:
- Format.Field constants associated with the text at the given position.
-
clone
public Object clone() throws CloneNotSupportedException
Creates a copy of the DefaultFormatter.- Overrides:
clonein classDefaultFormatter- Returns:
- copy of the DefaultFormatter
- Throws:
CloneNotSupportedException- if the receiver's class does not implement the interface Cloneable.
-
getActions
protected Action[] getActions()
IfgetSupportsIncrementreturns true, this returns two Actions suitable for incrementing/decrementing the value.- Overrides:
getActionsin classJFormattedTextField.AbstractFormatter- Returns:
- Array of Actions to install on JFormattedTextField
-
-