问题
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