MaskFormatter is used to format and edit strings. The behavior
of a MaskFormatter is controlled by way of a String mask
that specifies the valid characters that can be contained at a particular
location in the Document model. The following characters can
be specified:
| 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.isLetter or
Character.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
setInvalidCharacters and setValidCharacters
methods. setInvalidCharacters allows you to specify
which characters are not legal. setValidCharacters allows
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('_');
System.out.println(formatter.valueToString("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 MaskFormatter is configured to only allow valid characters
(setAllowsInvalid(false)) literal characters will be skipped as
necessary when editing. Consider a MaskFormatter with
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|212The '-' 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 MaskFormatter in the
previous example. Both inserts will result in the same String,
'123-45__'. When MaskFormatter
is processing the insert at character position 3 (the '-'), two things can
happen:
By default MaskFormatter will not allow invalid edits, you can
change this with the setAllowsInvalid method, and will
commit edits on valid edits (use the setCommitsOnValidEdit to
change this).
By default, MaskFormatter is 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 method setOverwriteMode.
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.beans package.
Please see java..
| Modifier and Type | Class and Description |
|---|---|
| private class | MaskFormatter.
Represents either a character or digit, uses
|
| private class | MaskFormatter.
Represents a letter, uses |
| private class | MaskFormatter.
Represents a number, uses |
| private class | MaskFormatter.
Represents a hex character, 0-9a-fA-F. a-f is mapped to A-F |
| private class | MaskFormatter.
Used to represent a fixed character in the mask. |
| private class | MaskFormatter.
Represents a character, upper case letters are mapped to lower case
using |
| private class | |
| private class | MaskFormatter.
Represents a character, lower case letters are mapped to upper case
using |
| Modifier and Type | Field and Description |
|---|---|
| private static final char | |
| private static final char | |
| private static final char | |
| private boolean | containsLiteralChars
Indicates if the value contains the literal characters. |
| private static final char | |
| private static final MaskFormatter. | |
| private static final char | |
| private String | invalidCharacters
List of invalid characters. |
| private static final char | |
| private static final char | |
| private String | mask
The user specified mask. |
| private transient MaskFormatter. | |
| private char | placeholder
String used to represent characters not present. |
| private String | placeholderString
String used for the passed in value if it does not completely fill the mask. |
| private static final char | |
| private String | validCharacters
List of valid characters. |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| private void | append(StringBuilder result, String value, int[] index, String placeholder, MaskFormatter.
Invokes |
| pack-priv boolean | canReplace(DefaultFormatter.
Overrides javax. This method does the following (assuming !getAllowsInvalid()): iterate over the max of the deleted region or the text length, for each character:
|
| private char | getCharacter(int index, char aChar)
Returns the character to insert at the specified location based on the passed in character. |
| public String | Returns: illegal characters.Returns the characters that are not valid for input. |
| private int | getInvalidOffset(String string, boolean completeMatch)
Returns -1 if the passed in string is valid, otherwise the index of the first bogus character is returned. |
| private char | |
| public String | |
| private MaskFormatter. | |
| private int | |
| public String | Returns: String used when formatting if the value does not completely fill the maskReturns the String to use if the value does not completely fill in the mask. |
| public char | Returns: Character used when formatting if the value does not completely fill the maskReturns the character to use in place of characters that are not present in the value, ie the user must fill them in. |
| public String | |
| public boolean | Returns: True if literal characters in mask should be returned in stringToValueReturns true if |
| public void | install(JFormattedTextField
JFormattedTextField to format for, may be null indicating
uninstall from current JFormattedTextField. ftf)Overrides javax. Installs the |
| private boolean | isLiteral(int index)
Returns true if the character at the specified location is a literal, that is it can not be edited. |
| pack-priv boolean | isNavigatable(int offset)
Overrides javax. Returns true if the MaskFormatter allows invalid, or
the offset is less than the max length and the character at
|
| private boolean | |
| private boolean | isValidCharacter(int index, char aChar)
Returns true if the passed in character matches the mask at the specified location. |
| pack-priv boolean | |
| private void | readObject(ObjectInputStream s)
Subclassed to update the internal representation of the mask after the default read operation has completed. |
| public void | setInvalidCharacters(String
If non-null, specifies illegal characters. invalidCharacters)Allows for further restricting of the characters that can be input. |
| public void | |
| public void | setPlaceholder(String
String used when formatting if the value does not
completely fill the mask placeholder)Sets the string to use if the value does not completely fill in the mask. |
| public void | setPlaceholderCharacter(char
Character used when formatting if the value does not
completely fill the mask 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. |
| public void | setValidCharacters(String
If non-null, specifies legal characters. validCharacters)Allows for further restricting of the characters that can be input. |
| public void | setValueContainsLiteralCharacters(boolean
Used to indicate if literal characters in
mask should be returned in stringToValue containsLiteralChars)If true, the returned value and set value will also contain the literal characters in mask. |
| public Object | Returns: Object representation of textString to convert value)Overrides javax. Parses the text, returning the appropriate Object representation of
the String |
| private Object | |
| private String | |
| private void | |
| public String | Returns: String representation of valueValue to convert value)Overrides javax. Returns a String representation of the Object |
| ALPHA_NUMERIC_KEY | back to summary |
|---|---|
| private static final char ALPHA_NUMERIC_KEY | |
| ANYTHING_KEY | back to summary |
|---|---|
| private static final char ANYTHING_KEY | |
| CHARACTER_KEY | back to summary |
|---|---|
| private static final char CHARACTER_KEY | |
| containsLiteralChars | back to summary |
|---|---|
| private boolean containsLiteralChars Indicates if the value contains the literal characters. | |
| DIGIT_KEY | back to summary |
|---|---|
| private static final char DIGIT_KEY | |
| EmptyMaskChars | back to summary |
|---|---|
| private static final MaskFormatter. | |
| HEX_KEY | back to summary |
|---|---|
| private static final char HEX_KEY | |
| invalidCharacters | back to summary |
|---|---|
| private String invalidCharacters List of invalid characters. | |
| LITERAL_KEY | back to summary |
|---|---|
| private static final char LITERAL_KEY | |
| LOWERCASE_KEY | back to summary |
|---|---|
| private static final char LOWERCASE_KEY | |
| mask | back to summary |
|---|---|
| private String mask The user specified mask. | |
| maskChars | back to summary |
|---|---|
| private transient MaskFormatter. | |
| placeholder | back to summary |
|---|---|
| private char placeholder String used to represent characters not present. | |
| placeholderString | back to summary |
|---|---|
| private String placeholderString String used for the passed in value if it does not completely fill the mask. | |
| UPPERCASE_KEY | back to summary |
|---|---|
| private static final char UPPERCASE_KEY | |
| validCharacters | back to summary |
|---|---|
| private String validCharacters List of valid characters. | |
| MaskFormatter | back to summary |
|---|---|
| public MaskFormatter() Creates a MaskFormatter with no mask. | |
| MaskFormatter | back to summary |
|---|---|
| public MaskFormatter(String mask) throws ParseException Creates a
| |
| append | back to summary |
|---|---|
| private void append(StringBuilder result, String value, int[] index, String placeholder, MaskFormatter. Invokes | |
| canReplace | back to summary |
|---|---|
| pack-priv boolean canReplace(DefaultFormatter. Overrides javax. This method does the following (assuming !getAllowsInvalid()): iterate over the max of the deleted region or the text length, for each character:
| |
| getCharacter | back to summary |
|---|---|
| private char getCharacter(int index, char aChar) Returns the character to insert at the specified location based on the passed in character. This provides a way to map certain sets of characters to alternative values (lowercase to uppercase...). | |
| getInvalidCharacters | back to summary |
|---|---|
| public String getInvalidCharacters() Returns the characters that are not valid for input.
| |
| getInvalidOffset | back to summary |
|---|---|
| private int getInvalidOffset(String string, boolean completeMatch) Returns -1 if the passed in string is valid, otherwise the index of the first bogus character is returned. | |
| getLiteral | back to summary |
|---|---|
| private char getLiteral(int index) Returns the literal character at the specified location. | |
| getMask | back to summary |
|---|---|
| public String getMask() Returns the formatting mask.
| |
| getMaskCharacter | back to summary |
|---|---|
| private MaskFormatter. Returns the MaskCharacter at the specified location. | |
| getMaxLength | back to summary |
|---|---|
| private int getMaxLength() Returns the maximum length the text can be. | |
| getPlaceholder | back to summary |
|---|---|
| public String getPlaceholder() Returns the String to use if the value does not completely fill in the mask.
| |
| getPlaceholderCharacter | back to summary |
|---|---|
| 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.
| |
| getValidCharacters | back to summary |
|---|---|
| public String getValidCharacters() Returns the valid characters that can be input.
| |
| getValueContainsLiteralCharacters | back to summary |
|---|---|
| public boolean getValueContainsLiteralCharacters() Returns true if
| |
| install | back to summary |
|---|---|
| public void install(JFormattedTextField ftf) Overrides javax. Installs the
Subclasses will typically only need to override this if they
wish to install additional listeners on the
If there is a
While this is a public method, this is typically only useful
for subclassers of
| |
| isLiteral | back to summary |
|---|---|
| private boolean isLiteral(int index) Returns true if the character at the specified location is a literal, that is it can not be edited. | |
| isNavigatable | back to summary |
|---|---|
| pack-priv boolean isNavigatable(int offset) Overrides javax. Returns true if the MaskFormatter allows invalid, or
the offset is less than the max length and the character at
| |
| isPlaceholder | back to summary |
|---|---|
| private boolean isPlaceholder(int index, char aChar) Returns true if the placeholder character matches aChar. | |
| isValidCharacter | back to summary |
|---|---|
| private boolean isValidCharacter(int index, char aChar) Returns true if the passed in character matches the mask at the specified location. | |
| isValidEdit | back to summary |
|---|---|
| pack-priv boolean isValidEdit(DefaultFormatter. | |
| readObject | back to summary |
|---|---|
| private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException Subclassed to update the internal representation of the mask after the default read operation has completed.
| |
| setInvalidCharacters | back to summary |
|---|---|
| public void setInvalidCharacters(String invalidCharacters) Allows for further restricting of the characters that can be input.
Only characters specified in the mask, not in the
| |
| setMask | back to summary |
|---|---|
| public void setMask(String mask) throws ParseException Sets the mask dictating the legal characters.
This will throw a
| |
| setPlaceholder | back to summary |
|---|---|
| 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.
| |
| setPlaceholderCharacter | back to summary |
|---|---|
| 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.
| |
| setValidCharacters | back to summary |
|---|---|
| public void setValidCharacters(String validCharacters) Allows for further restricting of the characters that can be input.
Only characters specified in the mask, not in the
| |
| setValueContainsLiteralCharacters | back to summary |
|---|---|
| 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
| |
| stringToValue | back to summary |
|---|---|
| public Object stringToValue(String value) throws ParseException Overrides javax. Parses the text, returning the appropriate Object representation of
the String
| |
| stringToValue | back to summary |
|---|---|
| private Object stringToValue(String value, boolean completeMatch) throws ParseException Actual | |
| stripLiteralChars | back to summary |
|---|---|
| private String stripLiteralChars(String string) Removes the literal characters from the passed in string. | |
| updateInternalMask | back to summary |
|---|---|
| private void updateInternalMask() throws ParseException Updates the internal representation of the mask. | |
| valueToString | back to summary |
|---|---|
| public String valueToString(Object value) throws ParseException Overrides javax. Returns a String representation of the Object
| |
Character.isLetterOrDigit.
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | isValidCharacter(char aChar)
Overrides javax. Returns true if |
| AlphaNumericCharacter | back to summary |
|---|---|
| private AlphaNumericCharacter() | |
| isValidCharacter | back to summary |
|---|---|
| public boolean isValidCharacter(char aChar) Overrides javax. Doc from javax. Returns true if | |
Character.isLetter.
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | isValidCharacter(char aChar)
Overrides javax. Returns true if |
| CharCharacter | back to summary |
|---|---|
| private CharCharacter() | |
| isValidCharacter | back to summary |
|---|---|
| public boolean isValidCharacter(char aChar) Overrides javax. Doc from javax. Returns true if | |
Character.isDigit.
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | isValidCharacter(char aChar)
Overrides javax. Returns true if |
| DigitMaskCharacter | back to summary |
|---|---|
| private DigitMaskCharacter() | |
| isValidCharacter | back to summary |
|---|---|
| public boolean isValidCharacter(char aChar) Overrides javax. Doc from javax. Returns true if | |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public char | getChar(char aChar)
Overrides javax. Returns the character to insert for |
| public boolean | isValidCharacter(char aChar)
Overrides javax. Returns true if |
| HexCharacter | back to summary |
|---|---|
| private HexCharacter() | |
| getChar | back to summary |
|---|---|
| public char getChar(char aChar) Overrides javax. Doc from javax. Returns the character to insert for | |
| isValidCharacter | back to summary |
|---|---|
| public boolean isValidCharacter(char aChar) Overrides javax. Doc from javax. Returns true if | |
| Modifier and Type | Field and Description |
|---|---|
| private char |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public char | getChar(char aChar)
Overrides javax. Returns the character to insert for |
| public boolean | isLiteral()
Overrides javax. Subclasses should override this returning true if the instance represents a literal character. |
| fixedChar | back to summary |
|---|---|
| private char fixedChar | |
| LiteralCharacter | back to summary |
|---|---|
| public LiteralCharacter(char fixedChar) | |
| getChar | back to summary |
|---|---|
| public char getChar(char aChar) Overrides javax. Doc from javax. Returns the character to insert for | |
| isLiteral | back to summary |
|---|---|
| public boolean isLiteral() Overrides javax. Doc from javax. Subclasses should override this returning true if the instance represents a literal character. The default implementation returns false. | |
Character.toLowerCase.
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public char | getChar(char aChar)
Overrides javax. Returns the character to insert for |
| public boolean | isValidCharacter(char aChar)
Overrides javax. Returns true if |
| LowerCaseCharacter | back to summary |
|---|---|
| private LowerCaseCharacter() | |
| getChar | back to summary |
|---|---|
| public char getChar(char aChar) Overrides javax. Doc from javax. Returns the character to insert for | |
| isValidCharacter | back to summary |
|---|---|
| public boolean isValidCharacter(char aChar) Overrides javax. Doc from javax. Returns true if | |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | append(StringBuilder buff, String formatting, int[] index, String placeholder)
Appends the necessary character in |
| public char | |
| public boolean | isLiteral()
Subclasses should override this returning true if the instance represents a literal character. |
| public boolean |
| MaskCharacter | back to summary |
|---|---|
| private MaskCharacter() | |
| append | back to summary |
|---|---|
| public void append(StringBuilder buff, String formatting, int[] index, String placeholder) throws ParseException Appends the necessary character in | |
| getChar | back to summary |
|---|---|
| public char getChar(char aChar) Returns the character to insert for | |
| isLiteral | back to summary |
|---|---|
| public boolean isLiteral() Subclasses should override this returning true if the instance represents a literal character. The default implementation returns false. | |
| isValidCharacter | back to summary |
|---|---|
| public boolean isValidCharacter(char aChar) Returns true if | |
Character.toUpperCase.
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public char | getChar(char aChar)
Overrides javax. Returns the character to insert for |
| public boolean | isValidCharacter(char aChar)
Overrides javax. Returns true if |
| UpperCaseCharacter | back to summary |
|---|---|
| private UpperCaseCharacter() | |
| getChar | back to summary |
|---|---|
| public char getChar(char aChar) Overrides javax. Doc from javax. Returns the character to insert for | |
| isValidCharacter | back to summary |
|---|---|
| public boolean isValidCharacter(char aChar) Overrides javax. Doc from javax. Returns true if | |