18

So I've been working on a Password Strength Checker and the way it works is that the user enters some random text into a textfield and then instantaneous visual feedback (breakdown of points) is displayed. I've also added a checkbox, which on being selected, should hide the password i.e. replace all the chars with asterisks, while preserving the actual text input by the user. A document listener is being used to keep track of changes inside the textfield. (each char on entry gets analyzed and then scored)

So, my question is, how exactly do I mask the user input with asterisks preserving its original value?

Here's what the GUI looks like:

http://speedcap.net/sharing/screen.php?id=files/51/2f/512f9abb3f92a25add7c593e9d80e9e4.png

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Manas Bajaj
  • 1,133
  • 3
  • 16
  • 26

4 Answers4

40

How exactly do I mask the user input with asterisks preserving its original value?

Use the JPasswordField which has nice function jPasswordField.getPassword(); to get the password as char[] to work with.

  • Use jPasswordField1.setEchoChar('*') to mask the password characters with *.
  • If you wish to see the value you are inserting use jPasswordField1.setEchoChar((char)0); Setting a value of 0 indicates that you wish to see the text as it is typed, similar to the behavior of a standard JTextField.

Tutorial and Reference:

  1. How to use Password Fields
  2. setEchoChar(char)
Eric
  • 6,563
  • 5
  • 42
  • 66
Sage
  • 15,290
  • 3
  • 33
  • 38
  • Set echo character to 0 to disable masking: `jPasswordField1.setEchoChar('\u0000')`. – Hollis Waite Nov 03 '13 at 17:12
  • @Sage, whenever I click on the hide checkBox, the textfield loses focus, and then I have to click on the textfield again for the changes to take place. I used to function requestFocus on it to regain focus, but it doesn't seem to work. What could I be doing wrong? – Manas Bajaj Nov 03 '13 at 17:39
  • @EnTHuSiAsTx94, please post another question with a short description, so that i and other experienced people from the community can help you with, it is also preferable to attach a short and simple working example which is able to produce your problem :)\ – Sage Nov 03 '13 at 17:48
3

Use Password Field Instead of using textfield

2

ok thanks for tutorialnya,

and ex,

action chechbox / double click

private void lihatActionPerformed(java.awt.event.ActionEvent evt) {  

   if (lihat.isSelected()) {
      password.setEchoChar((char)0); //password = JPasswordField
   } else {
      password.setEchoChar('*');
   }
}
Dumbo
  • 1,630
  • 18
  • 33
  • Welcome to Stack Overflow! This really seems like a comment, not an answer. With a bit more rep, [you will be able to post comments](http://stackoverflow.com/privileges/comment). – Nathan Tuggy Dec 26 '14 at 06:51
1

I solved it as follows:

if ( jPasswordField.getEchoChar() != '\u0000' ) {
    jPasswordField.setEchoChar('\u0000');
} else {
    jPasswordField.setEchoChar((Character) UIManager.get("PasswordField.echoChar"));
}