- java.lang.Object
-
- javax.swing.JFormattedTextField.AbstractFormatter
-
- javax.swing.text.DefaultFormatter
-
- javax.swing.text.MaskFormatter
-
- All Implemented Interfaces:
Serializable,Cloneable
public class MaskFormatter extends DefaultFormatter
MaskFormatteris used to format and edit strings. The behavior of aMaskFormatteris controlled by way of a String mask that specifies the valid characters that can be contained at a particular location in theDocumentmodel. The following characters can be specified:Valid characters and their descriptions Character Description # Any valid number, uses Character.isDigit.' Escape character, used to escape any of the special formatting characters. U Any character ( Character.isLetter). All lowercase letters are mapped to upper case.L Any character ( Character.isLetter). All upper case letters are mapped to lower case.A Any character or number ( Character.isLetterorCharacter.isDigit).? Any character ( Character.isLetter).* Anything. H Any hex character (0-9, a-f or A-F). Typically characters correspond to one char, but in certain languages this is not the case. The mask is on a per character basis, and will thus adjust to fit as many chars as are needed.
You can further restrict the characters that can be input by the
setInvalidCharactersandsetValidCharactersmethods.setInvalidCharactersallows you to specify which characters are not legal.setValidCharactersallows you to specify which characters are valid. For example, the following code block is equivalent to a mask of '0xHHH' with no invalid/valid characters:MaskFormatter formatter = new MaskFormatter("0x***"); formatter.setValidCharacters("0123456789abcdefABCDEF");When initially formatting a value if the length of the string is less than the length of the mask, two things can happen. Either the placeholder string will be used, or the placeholder character will be used. Precedence is given to the placeholder string. For example:
MaskFormatter formatter = new MaskFormatter("###-####"); formatter.setPlaceholderCharacter('_'); formatter.getDisplayValue(tf, "123");Would result in the string '123-____'. If
setPlaceholder("555-1212")was invoked '123-1212' would result. The placeholder String is only used on the initial format, on subsequent formats only the placeholder character will be used.If a
MaskFormatteris configured to only allow valid characters (setAllowsInvalid(false)) literal characters will be skipped as necessary when editing. Consider aMaskFormatterwith the mask "###-####" and current value "555-1212". Using the right arrow key to navigate through the field will result in (| indicates the position of the caret):|555-1212 5|55-1212 55|5-1212 555-|1212 555-1|212
The '-' is a literal (non-editable) character, and is skipped.Similar behavior will result when editing. Consider inserting the string '123-45' and '12345' into the
MaskFormatterin the previous example. Both inserts will result in the same String, '123-45__'. WhenMaskFormatteris processing the insert at character position 3 (the '-'), two things can happen:- If the inserted character is '-', it is accepted.
- If the inserted character matches the mask for the next non-literal character, it is accepted at the new location.
- Anything else results in an invalid edit
By default
MaskFormatterwill not allow invalid edits, you can change this with thesetAllowsInvalidmethod, and will commit edits on valid edits (use thesetCommitsOnValidEditto change this).By default,
MaskFormatteris in overwrite mode. That is as characters are typed a new character is not inserted, rather the character at the current location is replaced with the newly typed character. You can change this behavior by way of the methodsetOverwriteMode.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:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description MaskFormatter()Creates a MaskFormatter with no mask.MaskFormatter(String mask)Creates aMaskFormatterwith the specified mask.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringgetInvalidCharacters()Returns the characters that are not valid for input.StringgetMask()Returns the formatting mask.StringgetPlaceholder()Returns the String to use if the value does not completely fill in the mask.chargetPlaceholderCharacter()Returns the character to use in place of characters that are not present in the value, ie the user must fill them in.StringgetValidCharacters()Returns the valid characters that can be input.booleangetValueContainsLiteralCharacters()Returns true ifstringToValueshould return literal characters in the mask.voidinstall(JFormattedTextField ftf)Installs theDefaultFormatteronto a particularJFormattedTextField.voidsetInvalidCharacters(String invalidCharacters)Allows for further restricting of the characters that can be input.voidsetMask(String mask)Sets the mask dictating the legal characters.voidsetPlaceholder(String placeholder)Sets the string to use if the value does not completely fill in the mask.voidsetPlaceholderCharacter(char placeholder)Sets the character to use in place of characters that are not present in the value, ie the user must fill them in.voidsetValidCharacters(String validCharacters)Allows for further restricting of the characters that can be input.voidsetValueContainsLiteralCharacters(boolean containsLiteralChars)If true, the returned value and set value will also contain the literal characters in mask.ObjectstringToValue(String value)Parses the text, returning the appropriate Object representation of the Stringvalue.StringvalueToString(Object value)Returns a String representation of the Objectvaluebased on the mask.-
Methods declared in class javax.swing.text.DefaultFormatter
clone, getAllowsInvalid, getCommitsOnValidEdit, getDocumentFilter, getNavigationFilter, getOverwriteMode, getValueClass, setAllowsInvalid, setCommitsOnValidEdit, setOverwriteMode, setValueClass
-
Methods declared in class javax.swing.JFormattedTextField.AbstractFormatter
getActions, getFormattedTextField, invalidEdit, setEditValid, uninstall
-
-
-
-
Constructor Detail
-
MaskFormatter
public MaskFormatter()
Creates a MaskFormatter with no mask.
-
MaskFormatter
public MaskFormatter(String mask) throws ParseException
Creates aMaskFormatterwith the specified mask. AParseExceptionwill be thrown ifmaskis an invalid mask.- Parameters:
mask- the mask- Throws:
ParseException- if mask does not contain valid mask characters
-
-
Method Detail
-
setMask
public void setMask(String mask) throws ParseException
Sets the mask dictating the legal characters. This will throw aParseExceptionifmaskis not valid.- Parameters:
mask- the mask- Throws:
ParseException- if mask does not contain valid mask characters
-
getMask
public String getMask()
Returns the formatting mask.- Returns:
- Mask dictating legal character values.
-
setValidCharacters
public void setValidCharacters(String validCharacters)
Allows for further restricting of the characters that can be input. Only characters specified in the mask, not in theinvalidCharacters, and invalidCharacterswill be allowed to be input. Passing in null (the default) implies the valid characters are only bound by the mask and the invalid characters.- Parameters:
validCharacters- If non-null, specifies legal characters.
-
getValidCharacters
public String getValidCharacters()
Returns the valid characters that can be input.- Returns:
- Legal characters
-
setInvalidCharacters
public void setInvalidCharacters(String invalidCharacters)
Allows for further restricting of the characters that can be input. Only characters specified in the mask, not in theinvalidCharacters, and invalidCharacterswill be allowed to be input. Passing in null (the default) implies the valid characters are only bound by the mask and the valid characters.- Parameters:
invalidCharacters- If non-null, specifies illegal characters.
-
getInvalidCharacters
public String getInvalidCharacters()
Returns the characters that are not valid for input.- Returns:
- illegal characters.
-
setPlaceholder
public void setPlaceholder(String placeholder)
Sets the string to use if the value does not completely fill in the mask. A null value implies the placeholder char should be used.- Parameters:
placeholder- String used when formatting if the value does not completely fill the mask
-
getPlaceholder
public String getPlaceholder()
Returns the String to use if the value does not completely fill in the mask.- Returns:
- String used when formatting if the value does not completely fill the mask
-
setPlaceholderCharacter
public void setPlaceholderCharacter(char placeholder)
Sets the character to use in place of characters that are not present in the value, ie the user must fill them in. The default value is a space.This is only applicable if the placeholder string has not been specified, or does not completely fill in the mask.
- Parameters:
placeholder- Character used when formatting if the value does not completely fill the mask
-
getPlaceholderCharacter
public char getPlaceholderCharacter()
Returns the character to use in place of characters that are not present in the value, ie the user must fill them in.- Returns:
- Character used when formatting if the value does not completely fill the mask
-
setValueContainsLiteralCharacters
public void setValueContainsLiteralCharacters(boolean containsLiteralChars)
If true, the returned value and set value will also contain the literal characters in mask.For example, if the mask is
'(###) ###-####', the current value is'(415) 555-1212', andvalueContainsLiteralCharactersis truestringToValuewill return'(415) 555-1212'. On the other hand, ifvalueContainsLiteralCharactersis false,stringToValuewill return'4155551212'.- Parameters:
containsLiteralChars- Used to indicate if literal characters in mask should be returned in stringToValue
-
getValueContainsLiteralCharacters
public boolean getValueContainsLiteralCharacters()
Returns true ifstringToValueshould return literal characters in the mask.- Returns:
- True if literal characters in mask should be returned in stringToValue
-
stringToValue
public Object stringToValue(String value) throws ParseException
Parses the text, returning the appropriate Object representation of the Stringvalue. This strips the literal characters as necessary and invokes supersstringToValue, so that if you have specified a value class (setValueClass) an instance of it will be created. This will throw aParseExceptionif the value does not match the current mask. Refer tosetValueContainsLiteralCharacters(boolean)for details on how literals are treated.- Overrides:
stringToValuein classDefaultFormatter- Parameters:
value- String to convert- Returns:
- Object representation of text
- Throws:
ParseException- if there is an error in the conversion- See Also:
setValueContainsLiteralCharacters(boolean)
-
valueToString
public String valueToString(Object value) throws ParseException
Returns a String representation of the Objectvaluebased on the mask. Refer tosetValueContainsLiteralCharacters(boolean)for details on how literals are treated.- Overrides:
valueToStringin classDefaultFormatter- Parameters:
value- Value to convert- Returns:
- String representation of value
- Throws:
ParseException- if there is an error in the conversion- See Also:
setValueContainsLiteralCharacters(boolean)
-
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.
-
-