02 February, 2010

How to mask password field in Java?


Login windows use password masking as a technique to either hide the password as it is being typed or displaying a character (such as an asterisk '*') instead of the characters that users type. For example, when you login on a Windows machine, you are presented with a login dialog box in which the password field uses an asterisk as the masking or echo character.
If you wish to provide a graphical login dialog box for your application, you can use the JPasswordField, which allows the editing of a single line of text where the view indicates something was typed, but does not show the original characters. If you use the JPasswordField, the default echo character is an asterisk '*', but you can change it to any character of your choice. Again, if you set the echo character to zero, 0, it means that characters will be displayed as they are typed and no masking will be performed. 

example snippet of code:

JPasswordField password = new JPasswordField(8);
password.setEchoChar('#');

0 comments: