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 ….
- JPasswordField() –> Creates a default object of JPasswordField class.
- JPasswordField(int columns) –> Creats an object of JPasswordField class with specified number of columns.
- JPasswordField(String Text ) –> Creates an object of JPasswordField class with specified text.
- 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 …
- char getEchoChar() –> It is used for returning the character that is to be used for echoing.
- 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.
- 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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
//importing packages import javax.swing.*; import java.awt.*; // creating a class myPassField class myPassField { public static void main( String[] args) { // creating Frame and setting it's property JFrame f= new JFrame("Java Swing Password Field"); f.setVisible(true); f.setBounds(100,50,300,300); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // creating refernce of Container Container c=f.getContentPane(); // setting the layout manager as null c.setLayout(null); // creating object of JPasswordField class JPasswordField pwd=new JPasswordField(); //setting the location and size of JPasswordField pwd.setBounds(100,50,100,30); //adding it to the container using the add() method c.add(pwd); } } |
- Output :
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import javax.swing.*; import java.awt.*; class myPassField { public static void main( String[] args) { JFrame f= new JFrame("Java Swing Password Field"); f.setVisible(true); f.setBounds(100,50,300,300); Container c=f.getContentPane(); c.setLayout(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPasswordField pwd=new JPasswordField(); pwd.setBounds(100,50,100,30); //creating object of Font class Font fo= new Font("ARIAL",Font.BOLD,40); // applying it on the JPasswordField using the setFont() method pwd.setFont(fo); c.add(pwd); } } |
- Output :
Setting the Background Color of JPasswordField
- For Setting the Background color of JPasswordField, we will use a method setBackground().
- Programming Example is given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import javax.swing.*; import java.awt.*; class myPassField { public static void main( String[] args) { JFrame f= new JFrame("Java Swing Password Field"); f.setVisible(true); f.setBounds(100,50,300,300); Container c=f.getContentPane(); c.setLayout(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPasswordField pwd=new JPasswordField(); pwd.setBounds(100,50,100,30); Font fo= new Font("ARIAL",Font.BOLD,40); pwd.setFont(fo); //setting Background color of JPasswordField pwd.setBackground(Color.YELLOW); c.add(pwd); } } |
- Output :
Setting The Foreground Color of JPasswordField
- For Setting the Foreground color of JPasswordField, we will use the method setForeground().
- Programming example is given below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import javax.swing.*; import java.awt.*; class myPassField { public static void main( String[] args) { JFrame f= new JFrame("Java Swing Password Field"); f.setVisible(true); f.setBounds(100,50,300,300); Container c=f.getContentPane(); c.setLayout(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPasswordField pwd=new JPasswordField(); pwd.setBounds(100,50,100,30); Font fo= new Font("ARIAL",Font.BOLD,40); pwd.setFont(fo); pwd.setBackground(Color.YELLOW); //setting Foreground color of JPasswordField pwd.setForeground(Color.RED); c.add(pwd); } } |
- Output of the above program is given below :
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import javax.swing.*; import java.awt.*; class myPassField { public static void main( String[] args) { JFrame f= new JFrame("Java Swing Password Field"); f.setVisible(true); f.setBounds(100,50,300,300); Container c=f.getContentPane(); c.setLayout(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPasswordField pwd=new JPasswordField(); pwd.setBounds(100,50,100,30); Font fo= new Font("ARIAL",Font.BOLD,40); pwd.setFont(fo); pwd.setBackground(Color.YELLOW); pwd.setForeground(Color.RED); // setting Echo Character for JPasswordField pwd.setEchoChar('*'); c.add(pwd); } } |
- Output :
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import javax.swing.*; import java.awt.*; class myPassField { public static void main( String[] args) { JFrame f= new JFrame("Java Swing Password Field"); f.setVisible(true); f.setBounds(100,50,300,300); Container c=f.getContentPane(); c.setLayout(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPasswordField pwd=new JPasswordField(); pwd.setBounds(100,50,100,30); Font fo= new Font("ARIAL",Font.BOLD,40); pwd.setFont(fo); pwd.setBackground(Color.YELLOW); pwd.setForeground(Color.RED); // setting Echo Character for JPasswordField as '#' pwd.setEchoChar('#'); c.add(pwd); } } |
- Output of the above program is given below.
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.
i want detail and video of this tutorial can you provide me? :p
I enjoy what you guys tend to be up too. This type of clever work
and coverage! Keep up the good works guys I’ve incorporated you guys to our blogroll.
Thanx for the compliment !!!
I am really grateful to the owner of this web site who has shared this
impressive post at here.
Generally I do not learn post on blogs, but I wish to say that this write-up very
compelled me to try and do so! Your writing taste has been amazed me.
Thanks, very great article.
Good way of explaining, and nice paragraph to obtain information on the topic of
my presentation topic, which i am going to convey in college.
Do you have any video of that? I’d want to find out
more details.
Sorry brother i don’t have video but i will try to make one !
Hi there, after reading this awesome paragraph i am also glad to share my knowledge here with mates.
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!
Thank you so much !!