Creating Password Field by Using JPasswordField Class

In this tutorial, we are going to show you how to create password field using JPasswordField class.

The password field allows user to enter password. For security reason, password field displays echo characters instead of the password itself. The default echo character is (*).

In Java swing, to create a password field you use JPasswordField class. You can assign a different echo character other than the default one (*) using setEchoChar() method. You can get the password using getPassword() method. If you use copy() orcut() method, you will get nothing but beep sound.

The following table illustrates some common uses constructors of the JPasswordField class:

JPasswordField ConstructorsMeaning
public JPasswordField ()Creates a new password field.
public  JPasswordField (Document doc, String text, int columns)Creates a new password field with given document and number of columns.
public  JPasswordField(String text)Creates a new password field with a given text.
public  JPasswordField(int columns)Creates a new password field with a given columns.
public  JPasswordField(String text, int columns)Creates a new password field with given text and number of columns.

Example of using JPasswordField class

In this example, we will use JPasswordField class to create a simple password field.

JPasswordField Demo

In order to run the demo application, you’ll need SpringUtilities class. Click on the download link below to download file.

Java Swing Spring Utilities (4430 downloads)
  • Was this tutorial helpful ?
  • YesNo