The MaskFormatter Class
MaskFormatter
is a subclass of DefaultFormatter
that formats strings by matching them against a mask
. The mask
is a string of literals and nonliterals. The nonliterals (listed and
described in Table 20-4)
are wildcards that match a family of characters. Literals match only
themselves. A single quote preceding a nonliteral (or another single
quote) turns it into a literal. So, for example, the mask "ABa'A#''Hb"
consists of the nonliteral
A
, the literal B
, the literal a
, the literal A
, the nonliteral #
, the literal '
, the nonliteral H
, and the literal b
. The string "1BaA1'1b"
matches this mask.
Table 20-4. Mask nonliterals (case-sensitive)
char | Matches | Notes |
---|---|---|
* | Any character | |
A | Any alphanumeric character | Tested by |
? | Any alphabetic character | Tested by |
U | Uppercase alphabetic | Like |
L | Lowercase alphabetic | Like |
# | Any numeric character | Tested by |
H | Any hexadecimal numeric | Like |
' | Precedes any character in this table to create a literal |
The JFormattedTextField
in
Figure 19-1 (and the code
that follows it) uses a simple MaskFormatter
with mask
"UUUUU"
.
A MaskFormatter
installed on a
JFormattedTextField
controls the
caret so that by default it skips over literals and lands on
nonliterals, which is nice. It can also be configured (by setting the
valueContainsLiteralCharacters
property to false
) to have getValue( ...
Get Java Swing, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.