- java.lang.Object
-
- javax.swing.JFormattedTextField.AbstractFormatter
-
- javax.swing.text.DefaultFormatter
-
- All Implemented Interfaces:
- Serializable, Cloneable
- Direct Known Subclasses:
- InternationalFormatter, MaskFormatter
public class DefaultFormatter extends JFormattedTextField.AbstractFormatter implements Cloneable, Serializable
DefaultFormatter
formats aribtrary objects. Formatting is done by invoking thetoString
method. In order to convert the value back to a String, your class must provide a constructor that takes a String argument. If no single argument constructor that takes a String is found, the returned value will be the String passed intostringToValue
.Instances of
DefaultFormatter
can not be used in multiple instances ofJFormattedTextField
. To obtain a copy of an already configuredDefaultFormatter
, use theclone
method.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:
JFormattedTextField.AbstractFormatter
-
-
Constructor Summary
Constructors Constructor and Description DefaultFormatter()
Creates a DefaultFormatter.
-
Method Summary
Methods Modifier and Type Method and Description Object
clone()
Creates a copy of the DefaultFormatter.boolean
getAllowsInvalid()
Returns whether or not the value being edited is allowed to be invalid for a length of time.boolean
getCommitsOnValidEdit()
Returns when edits are published back to theJFormattedTextField
.protected DocumentFilter
getDocumentFilter()
Returns theDocumentFilter
used to restrict the characters that can be input into theJFormattedTextField
.protected NavigationFilter
getNavigationFilter()
Returns theNavigationFilter
used to restrict where the cursor can be placed.boolean
getOverwriteMode()
Returns the behavior when inserting characters.Class<?>
getValueClass()
Returns that class that is used to create new Objects.void
install(JFormattedTextField ftf)
Installs theDefaultFormatter
onto a particularJFormattedTextField
.void
setAllowsInvalid(boolean allowsInvalid)
Sets whether or not the value being edited is allowed to be invalid for a length of time (that is,stringToValue
throws aParseException
).void
setCommitsOnValidEdit(boolean commit)
Sets when edits are published back to theJFormattedTextField
.void
setOverwriteMode(boolean overwriteMode)
Configures the behavior when inserting characters.void
setValueClass(Class<?> valueClass)
Sets that class that is used to create new Objects.Object
stringToValue(String string)
Converts the passed in String into an instance ofgetValueClass
by way of the constructor that takes a String argument.String
valueToString(Object value)
Converts the passed in Object into a String by way of thetoString
method.-
Methods inherited from class javax.swing.JFormattedTextField.AbstractFormatter
getActions, getFormattedTextField, invalidEdit, setEditValid, uninstall
-
-
-
-
Method Detail
-
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 classJFormattedTextField.AbstractFormatter
- Parameters:
ftf
- JFormattedTextField to format for, may be null indicating uninstall from current JFormattedTextField.
-
setCommitsOnValidEdit
public void setCommitsOnValidEdit(boolean commit)
Sets when edits are published back to theJFormattedTextField
. If true,commitEdit
is invoked after every valid edit (any time the text is edited). On the other hand, if this is false than theDefaultFormatter
does not publish edits back to theJFormattedTextField
. As such, the only time the value of theJFormattedTextField
will change is whencommitEdit
is invoked onJFormattedTextField
, typically when enter is pressed or focus leaves theJFormattedTextField
.- Parameters:
commit
- Used to indicate when edits are commited back to the JTextComponent
-
getCommitsOnValidEdit
public boolean getCommitsOnValidEdit()
Returns when edits are published back to theJFormattedTextField
.- Returns:
- true if edits are commited after evey valid edit
-
setOverwriteMode
public void setOverwriteMode(boolean overwriteMode)
Configures the behavior when inserting characters. IfoverwriteMode
is true (the default), new characters overwrite existing characters in the model.- Parameters:
overwriteMode
- Indicates if overwrite or overstrike mode is used
-
getOverwriteMode
public boolean getOverwriteMode()
Returns the behavior when inserting characters.- Returns:
- true if newly inserted characters overwrite existing characters
-
setAllowsInvalid
public void setAllowsInvalid(boolean allowsInvalid)
Sets whether or not the value being edited is allowed to be invalid for a length of time (that is,stringToValue
throws aParseException
). It is often convenient to allow the user to temporarily input an invalid value.- Parameters:
allowsInvalid
- Used to indicate if the edited value must always be valid
-
getAllowsInvalid
public boolean getAllowsInvalid()
Returns whether or not the value being edited is allowed to be invalid for a length of time.- Returns:
- false if the edited value must always be valid
-
setValueClass
public void setValueClass(Class<?> valueClass)
Sets that class that is used to create new Objects. If the passed in class does not have a single argument constructor that takes a String, String values will be used.- Parameters:
valueClass
- Class used to construct return value from stringToValue
-
getValueClass
public Class<?> getValueClass()
Returns that class that is used to create new Objects.- Returns:
- Class used to constuct return value from stringToValue
-
stringToValue
public Object stringToValue(String string) throws ParseException
Converts the passed in String into an instance ofgetValueClass
by way of the constructor that takes a String argument. IfgetValueClass
returns null, the Class of the current value in theJFormattedTextField
will be used. If this is null, a String will be returned. If the constructor thows an exception, aParseException
will be thrown. If there is no single argument String constructor,string
will be returned.- Specified by:
stringToValue
in classJFormattedTextField.AbstractFormatter
- Parameters:
string
- String to convert- Returns:
- Object representation of text
- Throws:
ParseException
- if there is an error in the conversion
-
valueToString
public String valueToString(Object value) throws ParseException
Converts the passed in Object into a String by way of thetoString
method.- Specified by:
valueToString
in classJFormattedTextField.AbstractFormatter
- Parameters:
value
- Value to convert- Returns:
- String representation of value
- Throws:
ParseException
- if there is an error in the conversion
-
getDocumentFilter
protected DocumentFilter getDocumentFilter()
Returns theDocumentFilter
used to restrict the characters that can be input into theJFormattedTextField
.- Overrides:
getDocumentFilter
in classJFormattedTextField.AbstractFormatter
- Returns:
- DocumentFilter to restrict edits
-
getNavigationFilter
protected NavigationFilter getNavigationFilter()
Returns theNavigationFilter
used to restrict where the cursor can be placed.- Overrides:
getNavigationFilter
in classJFormattedTextField.AbstractFormatter
- Returns:
- NavigationFilter to restrict navigation
-
clone
public Object clone() throws CloneNotSupportedException
Creates a copy of the DefaultFormatter.- Overrides:
clone
in classJFormattedTextField.AbstractFormatter
- 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
-
-
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
21/11/2024 21:30:06 Cette version de la page est en cache (à la date du 21/11/2024 21:30:06) 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 30/08/2006, dernière modification le 04/03/2020
Source du document imprimé : https://www.gaudry.be/java-api-rf-javax/swing/text/defaultformatter.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
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.