Java Swing Password Field -JPasswordField Tutorial

Hello friends, Today, we will learn about how to create and use Java Swing Password Field . In previous tutorials, we learned about different-different classes within javax.swing package. If you did not see my previous tutorials, then go through my earlier posts. The link is given below…

And now, we will learn about another class, JPasswordField, which is defined within javax.swing package.

So let’s start our tutorial Java Swing Password Field.

Java Swing Password Field

What is JPasswordField ??

  • It is a class defined within javax.swing package.
  • It is just like a JTextField in which a user gives input.
  • The difference between a JTextField and JPasswordField is that whatever input we give in the textfield it’s original form doesn’t display instead of  it displays in the form of echo characters.
  • We can set an echo character for the JPasswordField using setEchoChar() method which takes a character as an argument.

Some of it’s constructors are given below ….

  1. JPasswordField() –> Creates a default object of JPasswordField class.
  2. JPasswordField(int columns) –> Creats an object of JPasswordField class with specified number of columns.
  3. JPasswordField(String Text ) –> Creates an object of JPasswordField class with specified text.
  4. JPasswordField(String Text,int columns ) –> Creates an object of JPasswordField class with specified text and number of columns.

Some of it’s methods are given below …

  1. char getEchoChar() –> It is used for returning the character that is to be used for echoing.
  2. void setEchoChar(char ch) –> It is used for setting the echo character used for password field. It takes a character as its argument .The default Echo character of a JPasswordField is “●”. We can set it to any other character using this method.
  3. char[] getPassword() –> It is used for getting the password contained in the password field .

So this was the brief description of JPasswordField class and now we will see how to create JPasswordField….

Creating JPasswordField

Steps

  • For creating JPasswordField, we need to create the object of the JPasswordField class. Example- JPasswordField pwd=new JPasswordField();
  • Next, we will set the location and size of JPasswordField using the setBounds() method, and then we will add it to the container.
  • Programming example is given below :
  • Output :
Java Swing Password Field
java-swing-password-field-fig-1

Setting the Font Style and Font Size of JPasswordField

Steps

  • To set the font style and font size of JPasswordField, we will follow the same process while dealing with JLabel.
  • Programming code is given below :
  • Output :
java-swing-password-field
java-swing-password-field-fig-2

Setting the Background Color of JPasswordField

  • For Setting the Background color of JPasswordField, we will use a method setBackground().
  • Programming Example is given below.
  • Output :
java-swing-password-field
java-swing-password-field-fig-3

Setting The Foreground Color of JPasswordField

  • For Setting the Foreground color of JPasswordField, we will use the method setForeground().
  • Programming example is given below :
  • Output of the above program is given below :
Java Swing Password Field
java-swing-password-field-fig-4

Setting the Echo Character for JPasswordField/ use of setEchoChar() method

  • For this, we need to call the method setEchoChar(), which takes a character as an argument. we will be calling this method using the object of JPasswordField class.
  • Programming example is given below :
  • Output  :
Java Swing Password Field
java-swing-password-field-fig-5
  • As we can see in the above image we have changed the Echo character to “*”.

Program Using jpasswordfield to Set the Password Character as ‘#’ instead of ‘*’

  • In this example, we will set the password character as ‘#” using the setEchoChar() method, which takes a character as an argument.
  • Programming example is given below.
  • Output of the above program is given below.
java swing password field – fig -6

So guys, this was all for this tutorial java swing password field . Please do comment and let me know if you are having any problems understanding this tutorial. Thank You.

11 thoughts on “Java Swing Password Field -JPasswordField Tutorial”

  1. This is the perfect site for anybody who wishes to understand this topic.

    You know so much its almost tough to argue with you (not that I actually will need to…HaHa).
    You certainly put a new spin on a topic which has been written about for many years.
    Wonderful stuff, just wonderful!

    Reply

Leave a Comment