I'm trying to implement something like a smart text field, in which I use a reg. ex. to provide some sort of a formatting "mask" on the user's input. Take, for example, a mask which would accept only social security numbers:
String ssn_pattern = "\\d{3}-\\d{2}-\\d{4}";
I could easily use a DocumentFilter with a Pattern Matcher with the hitEnd() method to see if I have a partial match, so I could reject invalid characters on a letter-by-letter basis as the user is typing. However, here's what I'd like to do:With each keystroke, I'd like the DocumentFilter to look at the pattern and retrieve an array of possible characters that could come next in the pattern. So, if the user types a "1" into the text field, the set of next valid characters would be {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. But, when the user types "123", the set of next valid characters would be {-}. There is only one element in this set, and so I'd like the DocumentFilter to automatically append this character onto the input sequence.
So effectively what I'll have is a text field that, as the user types, both validates a partial input sequence, as well as looks ahead one character, and if there is only one valid character that could come next, proactively types it in for the user.
The benefit would be that fields that require formatting could be keyed in without worrying about the symbols that should be interspersed in the text, while automatically adding and displaying those symbols to the user as he or she is typing.
Possible?


Sign In
Create Account


Back to top









