javax.swing.text

Class InternationalFormatter

public class InternationalFormatter extends DefaultFormatter

This extends {@link DefaultFormatter} so that the value to string conversion is done via a {@link Format} object. This allows various additional formats to be handled by JFormattedField.
Constructor Summary
InternationalFormatter()
Creates a new InternationalFormatter with no Format specified.
InternationalFormatter(Format format)
Creates a new InternationalFormatter that uses the specified Format object for value to string conversion.
Method Summary
Objectclone()
This creates and returns a clone of this Formatter.
protected Action[]getActions()
Returns the Actions that are supported by this Formatter.
Format.Field[]getFields(int offset)
Returns the {@link Format.Field} constants that are associated with the specified position in the text.
FormatgetFormat()
Returns the currently used Format object that is used to format the JFormattedField.
ComparablegetMaximum()
Returns the maximal value that is allowed by this Formatter.
ComparablegetMinimum()
Returns the minimal value that is allowed by this Formatter.
voidinstall(JFormattedTextField ftf)
Installs the formatter on the specified {@link JFormattedTextField}.
voidsetFormat(Format format)
Sets the Format object that is used to convert values to strings.
voidsetMaximum(Comparable maxValue)
Sets the maximum value that is allowed by this Formatter.
voidsetMinimum(Comparable minValue)
Sets the minimum value that is allowed by this Formatter.
ObjectstringToValue(String string)
Converts a String (from the JFormattedTextField input) to a value.
StringvalueToString(Object value)
Converts a value object into a String.

Constructor Detail

InternationalFormatter

public InternationalFormatter()
Creates a new InternationalFormatter with no Format specified.

InternationalFormatter

public InternationalFormatter(Format format)
Creates a new InternationalFormatter that uses the specified Format object for value to string conversion.

Parameters: format the Format object to use for value to string conversion

Method Detail

clone

public Object clone()
This creates and returns a clone of this Formatter.

Returns: a clone of this formatter

Throws: CloneNotSupportedException not thrown here, since cloning is supported

getActions

protected Action[] getActions()
Returns the Actions that are supported by this Formatter.

UNKNOWN: the JDK API docs say here: If getSupportsIncrement returns true, this returns two Actions suitable for incrementing/decrementing the value. The questsion is, which method getSupportsIncrement? There is no such method in the whole API! So we just call super.getActions here.

getFields

public Format.Field[] getFields(int offset)
Returns the {@link Format.Field} constants that are associated with the specified position in the text. If offset is not a valid location in the input field, an empty array of fields is returned.

Parameters: offset the position in the text from which we want to fetch the fields constants

Returns: the field values associated with the specified position in the text

getFormat

public Format getFormat()
Returns the currently used Format object that is used to format the JFormattedField.

Returns: the current Format

getMaximum

public Comparable getMaximum()
Returns the maximal value that is allowed by this Formatter. A null value means that there is no restriction.

Returns: the maximal value that is allowed by this Formatter or null if there is no restriction

getMinimum

public Comparable getMinimum()
Returns the minimal value that is allowed by this Formatter. A null value means that there is no restriction.

Returns: the minimal value that is allowed by this Formatter or null if there is no restriction

install

public void install(JFormattedTextField ftf)
Installs the formatter on the specified {@link JFormattedTextField}. This method does the following things: This method is typically not overridden by subclasses. Instead override one of the mentioned methods in order to customize behaviour.

Parameters: ftf the {@link JFormattedTextField} in which this formatter is installed

setFormat

public void setFormat(Format format)
Sets the Format object that is used to convert values to strings.

Parameters: format the Format to use for value to string conversion

See Also: Format

setMaximum

public void setMaximum(Comparable maxValue)
Sets the maximum value that is allowed by this Formatter. The maximum value is given as an object that implements the {@link Comparable} interface. If maxValue is null, then the Formatter has no restrictions at the upper end. If value class is not yet specified and maxValue is not null, then valueClass is set to the class of the maximum value.

Parameters: maxValue the maximum permissable value

See Also: Comparable

setMinimum

public void setMinimum(Comparable minValue)
Sets the minimum value that is allowed by this Formatter. The minimum value is given as an object that implements the {@link Comparable} interface. If minValue is null, then the Formatter has no restrictions at the lower end. If value class is not yet specified and minValue is not null, then valueClass is set to the class of the minimum value.

Parameters: minValue the minimum permissable value

See Also: Comparable

stringToValue

public Object stringToValue(String string)
Converts a String (from the JFormattedTextField input) to a value. This is achieved by invoking {@link Format#parseObject(String)} on the specified Format object. This implementation differs slightly from {@link DefaultFormatter}, it does:
  1. Convert the string to an Object using the Formatter.
  2. If a valueClass has been set, this object is passed to {@link DefaultFormatter#stringToValue(String)} so that the value has the correct type. This may or may not work correctly, depending on the implementation of toString() in the value class and if the class implements a constructor that takes one String as argument.
  3. If no {@link ParseException} has been thrown so far, we check if the value exceeds either minimum or maximum if one of those has been specified and throw a ParseException if it does.
  4. Return the value.
If no format has been specified, then {@link DefaultFormatter#stringToValue(String)} is invoked as fallback.

Parameters: string the string to convert

Returns: the value for the string

Throws: ParseException if the string cannot be converted into a value object (e.g. invalid input)

valueToString

public String valueToString(Object value)
Converts a value object into a String. This is done by invoking {@link Format#format(Object)} on the specified Format object. If no format is set, then {@link DefaultFormatter#valueToString(Object)} is called as a fallback.

Parameters: value the value to be converted

Returns: the string representation of the value

Throws: ParseException if the value cannot be converted