Search code examples
javaswingjtextfield

How do I hide the text that the user enters into a password JTextField?


I have a class that asks for a username and password to autenticate a user in the application.

Inside it, I have 2 JTextField objects, one for the username input and one for the password input.

JTextField userNameTextField = new JTextField(20);
JTextField pswdTextField = new JTextField(20);

It works fine but my problem is that, when the user writes the password in the pswdTextField, the password is shown inside it. I want to hide it by showing a dot, asterisk or similar for each letter that makes up the inserted password.

How is this done?


Solution

  • Instead of using a JTextField, use a JPasswordField for your pswdTextField