In this tutorial you will learn about the Swing JPasswordField and its application with practical example.
Swing JPasswordField
JPasawordField is a subclass of JTextField . It’s a lightweight single line component which allows users for editing where the view indicates something was typed , but does not show the original character.
JPasswordField Class Declaration
1 |
public class JPasswordField extends JTextField |
JPasswordField Constructor
Some important and usable constructor summary listed below.
JPasswordField(): This constructor is used to construct a new Password Field.
JPasswordField(Document doc, String text, int columns): This constructor is used to construct a Password Field that uses the given text storage model and the given number of columns.
JPasswordField(int columns): This constructor is used to construct an empty Password Field with specific number of columns.
JPasswordField(String text): This constructor is used to construct a Password field with given string.
JPasswordField(String text, int columns): This constructor is used to construct a Password field with given string and columns.
JPasswordField Method
Some of Useable and important method are listed below.
char[] getPassword(): This method is used to return text of this component.
String getText(): This method is Deprecated , used to get Text of this component.
AccessibleContext getAccessibleContext(): This method returns AccessibleContext which is associated with this JPasswordField.
protected String paramString(): This method returns a string representation of this JTextField.
Example :-
In the below example I have used simple password field so that we can easily understand what does it mean.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package w3addaswing; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; public class W3ADDASwing { public static void main(String[] args) { // TODO code application logic here JFrame frame = new JFrame("JPasswordField Example W3ADDA"); JPasswordField pwd = new JPasswordField(); JLabel lbl=new JLabel("Enter Password"); lbl.setBounds(20,100,120,30); pwd.setBounds(120, 100, 100, 30); frame.add(lbl); frame.add(pwd); frame.setSize(400, 400); frame.setLayout(null); frame.setVisible(true); } } |
Output :-
Just copy this code or run it, you will get your result like above image.