javax.swing.text

Class InternationalFormatter

  • All Implemented Interfaces:
    Serializable, Cloneable
    Direct Known Subclasses:
    DateFormatter, NumberFormatter

    public class InternationalFormatter
    extends DefaultFormatter
    InternationalFormatter extends DefaultFormatter, using an instance of java.text.Format to handle the conversion to a String, and the conversion from a String.

    If getAllowsInvalid() is false, this will ask the Format to format the current text on every edit.

    You can specify a minimum and maximum value by way of the setMinimum and setMaximum methods. In order for this to work the values returned from stringToValue must be comparable to the min/max values by way of the Comparable interface.

    Be careful how you configure the Format and the InternationalFormatter, as it is possible to create a situation where certain values can not be input. Consider the date format 'M/d/yy', an InternationalFormatter 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 the JFormattedTextField being completely reset from the Format. The cursor position will also be adjusted as literal characters are added/removed from the resulting String.

    InternationalFormatter's behavior of stringToValue is slightly different than that of DefaultTextFormatter, it does the following:

    1. parseObject is invoked on the Format specified by setFormat
    2. If a Class has been set for the values (setValueClass), supers implementation is invoked to convert the value returned from parseObject to the appropriate class.
    3. If a ParseException has not been thrown, and the value is outside the min/max a ParseException is thrown.
    4. The value is returned.
    InternationalFormatter implements stringToValue in this manner so that you can specify an alternate Class than Format 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 see XMLEncoder.

    Since:
    1.4
    See Also:
    Format, Comparable
    • Constructor Detail

      • InternationalFormatter

        public InternationalFormatter()
        Creates an InternationalFormatter with no Format specified.
      • InternationalFormatter

        public InternationalFormatter(Format format)
        Creates an InternationalFormatter with the specified Format 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 the valueClass has not been specified, and minimum is non null, the valueClass will be set to that of the class of minimum.
        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 the valueClass has not been specified, and max is non null, the valueClass will be set to that of the class of max.
        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 the DefaultFormatter onto a particular JFormattedTextField. This will invoke valueToString to convert the current value from the JFormattedTextField to a String. This will then install the Actions from getActions, the DocumentFilter returned from getDocumentFilter and the NavigationFilter returned from getNavigationFilter onto the JFormattedTextField.

        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 the JFormattedTextField 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 class DefaultFormatter
        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 Object value. This invokes format on the current Format.
        Overrides:
        valueToString in class DefaultFormatter
        Parameters:
        value - Value to convert
        Returns:
        String representation of value
        Throws:
        ParseException - if there is an error in the conversion
      • getFields

        public Format.Field[] getFields(int offset)
        Returns the Format.Field constants associated with the text at offset. If offset 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.
      • getActions

        protected Action[] getActions()
        If getSupportsIncrement returns true, this returns two Actions suitable for incrementing/decrementing the value.
        Overrides:
        getActions in class JFormattedTextField.AbstractFormatter
        Returns:
        Array of Actions to install on JFormattedTextField

Traduction non disponible

Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.

Version en cache

28/12/2024 22:43:24 Cette version de la page est en cache (à la date du 28/12/2024 22:43:24) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.

Document créé le 01/09/2006, dernière modification le 04/03/2020
Source du document imprimé : https://www.gaudry.be/java-api-rf-javax/swing/text/InternationalFormatter.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.

Références

  1. Consulter le document html Langue du document :fr Manuel PHP : https://docs.oracle.com, InternationalFormatter

Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.

Table des matières Haut