how to define an eclipse mask formatted text field?

醉酒当歌 提交于 2019-12-11 19:44:52

问题


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, uses Character.isDigit.

If you want to accept text (letters and digits) use

A Any character or number (Character.isLetter or Character.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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!