java jpasswordfield getpassword

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class Frame extends JFrame implements ActionListener{
 
    private JPasswordField jpf;
    private JTextField jtf;
    private JButton jb;
    private String name = "",pass = "";
    private char getpass[];

    Frame()
    {
        setTitle("JPasswordField with ActionListener");
        setLayout(new FlowLayout());
        setJTextFieldandJPasswordField();
        setAction();
        setSize(700, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 
    private void setJTextFieldandJPasswordField()
    {
        JLabel     username = new JLabel("User name");
        jtf = new JTextField("User name",10);
        JLabel     password = new JLabel("Password");
        jpf = new JPasswordField(10);
        jb = new JButton("Enter");
        JLabel     nameAndPass = new JLabel("User name  :  HAJSOF    Password  :  123456");
        add(username);
        add(jtf);
        add(password);
        add(jpf);
        add(jb);
        add(nameAndPass);
    }
 
    private void setAction()
    {
        jb.addActionListener(this);
    }
 
    public void actionPerformed(ActionEvent eve)
    {
        name = jtf.getText();
        getpass = jpf.getPassword();
        pass = String.valueOf(getpass);
     
        if(name.equals("HAJSOF") && pass.equals("123456"))
            JOptionPane.showMessageDialog(rootPane, "Login Successful");
        else
            JOptionPane.showMessageDialog(rootPane, "Username or Password incorrect");
    }
}

public class Javaapp {
 
    public static void main(String[] args) {
     
        Frame frame = new Frame();
    }
}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source