SEARCH HERE

Tuesday, December 27, 2022

Java JPasswordField

 

Java JPasswordField

The object of a JPasswordField class is a text component specialized for password entry. It allows the editing of a single line of text. It inherits JTextField class.


JPasswordField class declaration

Let's see the declaration for javax.swing.JPasswordField class.

  1. public class JPasswordField extends JTextField  

Commonly used Constructors:

ConstructorDescription
JPasswordField()Constructs a new JPasswordField, with a default document, null starting text string, and 0 column width.
JPasswordField(int columns)Constructs a new empty JPasswordField with the specified number of columns.
JPasswordField(String text)Constructs a new JPasswordField initialized with the specified text.
JPasswordField(String text, int columns)Construct a new JPasswordField initialized with the specified text and columns.

Java JPasswordField Example

  1. import javax.swing.*;    
  2. public class PasswordFieldExample {  
  3.     public static void main(String[] args) {    
  4.     JFrame f=new JFrame("Password Field Example");    
  5.      JPasswordField value = new JPasswordField();   
  6.      JLabel l1=new JLabel("Password:");    
  7.         l1.setBounds(20,10080,30);    
  8.          value.setBounds(100,100,100,30);    
  9.             f.add(value);  f.add(l1);  
  10.             f.setSize(300,300);    
  11.             f.setLayout(null);    
  12.             f.setVisible(true);     
  13. }  
  14. }  

Output:

Java Jpasswardfield 1

Java JPasswordField Example with ActionListener

  1. import javax.swing.*;    
  2. import java.awt.event.*;  
  3. public class PasswordFieldExample {  
  4.     public static void main(String[] args) {    
  5.     JFrame f=new JFrame("Password Field Example");    
  6.      final JLabel label = new JLabel();            
  7.      label.setBounds(20,150200,50);  
  8.      final JPasswordField value = new JPasswordField();   
  9.      value.setBounds(100,75,100,30);   
  10.      JLabel l1=new JLabel("Username:");    
  11.         l1.setBounds(20,2080,30);    
  12.         JLabel l2=new JLabel("Password:");    
  13.         l2.setBounds(20,7580,30);    
  14.         JButton b = new JButton("Login");  
  15.         b.setBounds(100,12080,30);    
  16.         final JTextField text = new JTextField();  
  17.         text.setBounds(100,20100,30);    
  18.                 f.add(value); f.add(l1); f.add(label); f.add(l2); f.add(b); f.add(text);  
  19.                 f.setSize(300,300);    
  20.                 f.setLayout(null);    
  21.                 f.setVisible(true);     
  22.                 b.addActionListener(new ActionListener() {  
  23.                 public void actionPerformed(ActionEvent e) {       
  24.                    String data = "Username " + text.getText();  
  25.                    data += ", Password: "   
  26.                    + new String(value.getPassword());   
  27.                    label.setText(data);          
  28.                 }  
  29.              });   
  30. }  
  31. }  

Output:


Java Jpasswardfield 2

0 comments:

Post a Comment

C++

AJAVA

C

E-RESOURCES

LKG, UKG Live Worksheets

Top