- 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
InternationalFormatter
extendsDefaultFormatter
, using an instance ofjava.text.Format
to handle the conversion to a String, and the conversion from a String.If
getAllowsInvalid()
is false, this will ask theFormat
to format the current text on every edit.You can specify a minimum and maximum value by way of the
setMinimum
andsetMaximum
methods. In order for this to work the values returned fromstringToValue
must be comparable to the min/max values by way of theComparable
interface.Be careful how you configure the
Format
and theInternationalFormatter
, as it is possible to create a situation where certain values can not be input. Consider the date format 'M/d/yy', anInternationalFormatter
that 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
InternationalFormatter
is configured to only allow valid values (setAllowsInvalid(false)
), every valid edit will result in the text of theJFormattedTextField
being completely reset from theFormat
. The cursor position will also be adjusted as literal characters are added/removed from the resulting String.InternationalFormatter
's behavior ofstringToValue
is slightly different than that ofDefaultTextFormatter
, it does the following:parseObject
is invoked on theFormat
specified bysetFormat
- If a Class has been set for the values (
setValueClass
), supers implementation is invoked to convert the value returned fromparseObject
to the appropriate class. - If a
ParseException
has not been thrown, and the value is outside the min/max aParseException
is thrown. - The value is returned.
InternationalFormatter
implementsstringToValue
in this manner so that you can specify an alternate Class thanFormat
may 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 JavaBeansTM has been added to the
java.beans
package. Please seeXMLEncoder
.- Since:
- 1.4
- See Also:
Format
,Comparable
-
-
Constructor Summary
Constructors Constructor and Description InternationalFormatter()
Creates anInternationalFormatter
with noFormat
specified.InternationalFormatter(Format format)
Creates anInternationalFormatter
with the specifiedFormat
instance.
-
Method Summary
Methods Modifier and Type Method and Description Object
clone()
Creates a copy of the DefaultFormatter.protected Action[]
getActions()
IfgetSupportsIncrement
returns true, this returns two Actions suitable for incrementing/decrementing the value.Format.Field[]
getFields(int offset)
Returns theFormat.Field
constants associated with the text atoffset
.Format
getFormat()
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.void
install(JFormattedTextField ftf)
Installs theDefaultFormatter
onto a particularJFormattedTextField
.void
setFormat(Format format)
Sets the format that dictates the legal values that can be edited and displayed.void
setMaximum(Comparable max)
Sets the maximum permissible value.void
setMinimum(Comparable minimum)
Sets the minimum permissible value.Object
stringToValue(String text)
Returns theObject
representation of theString
text
.String
valueToString(Object value)
Returns a String representation of the Objectvalue
.-
Methods inherited from class javax.swing.text.DefaultFormatter
getAllowsInvalid, getCommitsOnValidEdit, getDocumentFilter, getNavigationFilter, getOverwriteMode, getValueClass, setAllowsInvalid, setCommitsOnValidEdit, setOverwriteMode, setValueClass
-
Methods inherited from class javax.swing.JFormattedTextField.AbstractFormatter
getFormattedTextField, invalidEdit, setEditValid, uninstall
-
-
-
-
Constructor Detail
-
InternationalFormatter
public InternationalFormatter()
Creates anInternationalFormatter
with noFormat
specified.
-
InternationalFormatter
public InternationalFormatter(Format format)
Creates anInternationalFormatter
with the specifiedFormat
instance.- 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
-Format
instance 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 thevalueClass
has not been specified, andminimum
is non null, thevalueClass
will 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 thevalueClass
has not been specified, andmax
is non null, thevalueClass
will 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 theDefaultFormatter
onto a particularJFormattedTextField
. This will invokevalueToString
to convert the current value from theJFormattedTextField
to a String. This will then install theAction
s fromgetActions
, theDocumentFilter
returned fromgetDocumentFilter
and theNavigationFilter
returned fromgetNavigationFilter
onto theJFormattedTextField
.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 theJFormattedTextField
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.- Overrides:
install
in 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 invokesformat
on the currentFormat
.- Overrides:
valueToString
in 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 theObject
representation of theString
text
.- Overrides:
stringToValue
in classDefaultFormatter
- Parameters:
text
-String
to convert- Returns:
Object
representation of text- Throws:
ParseException
- if there is an error in the conversion
-
getFields
public Format.Field[] getFields(int offset)
Returns theFormat.Field
constants associated with the text atoffset
. Ifoffset
is 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:
clone
in classDefaultFormatter
- Returns:
- copy of the DefaultFormatter
- Throws:
CloneNotSupportedException
- if the object's class does not support theCloneable
interface. Subclasses that override theclone
method can also throw this exception to indicate that an instance cannot be cloned.- See Also:
Cloneable
-
getActions
protected Action[] getActions()
IfgetSupportsIncrement
returns true, this returns two Actions suitable for incrementing/decrementing the value.- Overrides:
getActions
in classJFormattedTextField.AbstractFormatter
- Returns:
- Array of Actions to install on JFormattedTextField
-
-
Deutsche Übersetzung
Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.
Vielen Dank im Voraus.
Dokument erstellt 11/06/2005, zuletzt geändert 04/03/2020
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/java-api-rf-javax/swing/text/InternationalFormatter.html
Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.
Referenzen
Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.