问题
I need to put a masked formatted text field in my jframe, i put it like this
MaskFormatter mask = new MaskFormatter("########/##");
JFormattedTextField txtName = new JFormattedTextField(mask);
but when I run the program the textField is empty and it doesn't save it on data base
回答1:
From MaskFormatter documentation we can see that
#
Any valid number, usesCharacter.isDigit
.
If you want to accept text (letters and digits) use
A
Any character or number (Character.isLetter
orCharacter.isDigit
)*
Anything
You may also want to use this form
JFormattedTextField txtName = new JFormattedTextField();
MaskFormatter mask = new MaskFormatter("########/##");
mask.install(txtName);
回答2:
You say that it does not accept the text.
But # indicates only numbers
Here is a table extracted from: http://www.javalobby.org/java/forums/t48584.html
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).
来源:https://stackoverflow.com/questions/32018509/how-to-define-an-eclipse-mask-formatted-text-field