Mask the IP Address when the user enters in text field Java Swing

故事扮演 提交于 2019-12-11 07:38:06

问题


I have a JTextField to accommodate an ip address with 3 dots. 255.120.320.123. When the user enters this IP address, I want to mask it like ... I was referring this thread, How to custom formatter JFormattedTextField to display an IP address?

jFormattedTextField did not work for me. Can anyone give me an example with jFormattedTextField with 3 dots visible?

Or do I need to use 4 jFomattedTextField/JPasswordField as mentioned in this thread?

Thanks in advance.


回答1:


Seems you need to use MaskFormatter ,for example:

try {
    MaskFormatter mf = new MaskFormatter("###.###.###.###");
    JFormattedTextField f = new JFormattedTextField(mf);
    add(f);
} catch (ParseException e) {
    e.printStackTrace();
}




回答2:


I assume your trying to use JFormattedTextField? Maybe you should combine it with MaskFormatter.

Something like: // IPv4 like 192.168.1.1

   MaskFormatter formatter = new MaskFormatter("###.###.###.###");
 JFormattedTextField textField = new JFormattedTextField(formatter);

Here is an Example and guide



来源:https://stackoverflow.com/questions/22040845/mask-the-ip-address-when-the-user-enters-in-text-field-java-swing

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