Limiting and formatting a JTextfield

人走茶凉 提交于 2019-12-20 06:28:22

问题


I have a JTextfield where a user enters a "social security number". The SSN has 9 digits and 2 dashes for a total of 11 characters.

How do I limit the JTextField to 11 characters and also have 2 dashes in the textfield at the 4th and 7th position of the 11 characters?


回答1:


You might want to use : JFormattedTextField

See this for more help : How to Use Formatted Text Fields

Also this might help : MaskFormatter

Here is a sample snippet :

MaskFormatter formatter = new MaskFormatter("####-###-####");

EDIT :

After some research I found out that a DocumentFilter would do things better,

See this for more details : Implementing a Document Filter



来源:https://stackoverflow.com/questions/8199866/limiting-and-formatting-a-jtextfield

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