2

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.

Community
  • 1
  • 1
user1631072
  • 51
  • 2
  • 9

2 Answers2

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();
}

enter image description here

alex2410
  • 10,904
  • 3
  • 25
  • 41
0

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

Abdelsalam Shahlol
  • 1,621
  • 1
  • 20
  • 31
Nathan Cooper
  • 6,262
  • 4
  • 36
  • 75
  • while entering ip address 255.255.123.122 in the textfield,these values are visible. I wanted to mask it like if i use the JPasswordField instead of showing it as 255.255.123.122 in GUI, wanted to show as ***.***.***.*** I am referring this http://stackoverflow.com/questions/5339702/is-there-an-alternative-to-jpasswordfield – user1631072 Feb 26 '14 at 12:29
  • @user1631072 Oh I see, that is a tad different. Swap out the JFormattedTextField for a [JPasswordField](http://docs.oracle.com/javase/7/docs/api/javax/swing/JPasswordField.html) (and use echo char etc). I believe you can still use a MaskFormatter with one of those (maybe...) – Nathan Cooper Feb 26 '14 at 13:06
  • Hi Nathan, Thank you for your reply. Is there a way to use JPasswordField? The problem I am facing is, currently in a textfield displaying 3spaces.3spaces.3spaces.3spaces to enter the ip address. when I use the echochar with JPasswordField it takes space and . to * . So I am getting like *********** instead of ***.***.***.*** Once I select the checkbox mask, it is masking the spaces and dot. Before the user enters the ip address, echochar masking the spaces and dot to * . – user1631072 Feb 27 '14 at 06:09
  • You may have to go back JFormattedTextField and have the chars you want converted to * by using ActionListener or KeyListener. I'll have a look into this if i have time. – Nathan Cooper Feb 27 '14 at 09:04
  • char[] pass = tf1.getPassword(); tf1.setEchoChar('*'); for(int i =0;i – user1631072 Feb 27 '14 at 12:08