It is a subclass of JTextField class. This field doesn’t show characters and put * (asterisk) at the place of entered characters. It hides text behind asterisk (*).
Purpose of JPasswordField
JPasswordField adds extra security to your apps. While typing a password or setting a new password, it is compulsory that your password is hidden from others. If you don’t use JPasswordField then your password might be viewed by others.
Programming Example
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 26 27 28 29 | package Test.MainJava.com; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JPasswordField; public class JPasswordField_Example { JFrame frame; JPasswordField pwdfield; public static void main(String[] args) { JPasswordField_Example j=new JPasswordField_Example(); j.Draw(); } public void Draw() { pwdfield=new JPasswordField(20); //Initialize //Adding Component to Frame frame = new JFrame("Simple JPasswordField Example"); frame.setSize(300, 200); frame.setLayout(new FlowLayout()); frame.add(pwdfield); frame.setVisible(true); } } |
Formatting JPasswordField
You can set custom characters instead of ‘*’.
Getting Password from JPasswordField
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package Test.MainJava.com; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.lang.reflect.Array; import java.util.Arrays; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; public class JPasswordField_Example { JFrame frame; JPasswordField pwdfield; JLabel lblStatus; JButton btnGet; public static void main(String[] args) { JPasswordField_Example j=new JPasswordField_Example(); j.Draw(); } public void Draw() { pwdfield=new JPasswordField(20); //Initialize lblStatus=new JLabel(); btnGet=new JButton("Display Password"); btnGet.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub char[] pwd = pwdfield.getPassword(); String p=""; for(int i=0; i<pwd.length; i++) { p += pwd[i]; } lblStatus.setText(p); Arrays.fill(pwd, '0'); } }); //Adding Component to Frame frame = new JFrame("Simple JPasswordField Example"); frame.setSize(300, 200); frame.setLayout(new FlowLayout()); frame.add(pwdfield); frame.add(btnGet); frame.add(lblStatus); frame.setVisible(true); } } |
Arrays.fill()
method to remove password from char array for enhanced security.Don’t use following methods to get a password from JPasswordField component.
1 2 | String pwd=new String(pwdfield.getText()); //Wrong String pwdd= new String(pwdfield.getPassword()); //Wrong |
It is vulnerable to use String to gather password from JPasswordField
area. You always use Char[]
array to getting a password from JPasswordField
.
Adding Event Listener and Key Listener to JPasswordField
You can add Event Listener and Key Listener in JPasswordField in the following manner.
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | package Test.MainJava.com; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; public class JPasswordField_Example { JFrame frame; JPasswordField pwdfield; JLabel lblStatus; public static void main(String[] args) { JPasswordField_Example j=new JPasswordField_Example(); j.Draw(); } public void Draw() { pwdfield=new JPasswordField(20); //Initialize lblStatus=new JLabel(); //On Press Enter pwdfield.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub lblStatus.setText("You Just Pressed Enter"); } }); //On KeyPress, KeyReleased and KeyTyped pwdfield.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub lblStatus.setText("You Typed: " + e.getKeyChar()); } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub } }); //Adding Component to Frame frame = new JFrame("Simple JPasswordField Example"); frame.setSize(300, 200); frame.setLayout(new FlowLayout()); frame.add(pwdfield); frame.add(lblStatus); frame.setVisible(true); } } |
Output
Summary
In this tutorial, you learned how to use JPasswordField
in Swing to get or set the user password. In the next chapter, you will learn JTextArea
in Java Swing.