Java JPasswordField
JPasswordField class is a text component that allows single line text just like JTextField.It's used to enter password.
How to Declare JPasswordField class
This is how you declare java.swing.JPasswordField class.
[public class JPasswordField extends JTextField]
JPasswordField used constructors
- JPasswordField()-creates a JPasswordField with null text.
- JPasswordField(String s)-creates a JPasswordField with a text.
- JPasswordField(int columns)-creates an empty JPasswordField with specified number of columns
- JPasswordField(int columns, Strings)-creates a JPasswordField with specified number of columns and a text.
JPasswordField Example
[
import javax.swing.JFrame;
import javax.swing.JPasswordField; import javax.swing.JLabel; public class JPasswordFieldExample extends JFrame { JPasswordFieldExample() { super("JPasswordFieldExample"); setLayout(null); setLocationRelativeTo(null); JLabel label=new JLabel("Password"); JPasswordField pass=new JPasswordField(); label.setBounds(20,100, 80,30); pass.setBounds(100,100,100,30); add(label); add(pass); setSize(400,400); setVisible(true); } public static void main(String args[]) { new JPasswordFieldExample(); }
}
]
Ouput
JPasswordField with ActionListener Example
[
]import javax.swing.JFrame; import javax.swing.JPasswordField; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.UIManager; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JOptionPane; public class JPasswordFieldExample extends JFrame implements ActionListener{ public JPasswordField pass; public JTextField user; JPasswordFieldExample() { super("JPasswordFieldExample"); try { UIManager.setLookAndFeel ("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); } catch(Exception e) { e.printStackTrace(); } setLayout(null); setLocationRelativeTo(null); pass = new JPasswordField(); pass.setBounds(100,75,100,30); JLabel label1=new JLabel("Username:"); label1.setBounds(20,20, 80,30); JLabel label2=new JLabel("Password:"); label2.setBounds(20,75, 80,30); JButton button = new JButton("Login"); button.setToolTipText("Click here to login"); button.addActionListener(this); button.setBounds(100,120, 80,30); user = new JTextField(); user.setBounds(100,20, 100,30); add(label1); add(label2); add(user); add(pass); add(button); setSize(400,400); setVisible(true); } public static void main(String args[]) { new JPasswordFieldExample(); } public void actionPerformed(ActionEvent event) { String username=user.getText(); String password= (String)pass.getText(); JOptionPane.showMessageDialog(this, "Username Entered is="+username+ "\n Password is="+password,"Simple Login", JOptionPane.INFORMATION_MESSAGE); } }
Output
Lessons to learn from Above Program
I have introduced several methods and swing components that i have talked about in my previous posts.These are.
Main Lesson
The main lesson to learn today is how to create a simple login form with username and JPasswordField.
In my next lessons i will connect the login form with a database that verifies login credentials entered by the user.