2

I want to check the username and password in Swing.

The check works for username, but it doesn't work for JPaswordfield. I am posting the related code:

//Here I get the password
Char[] pwd = jt1.getPassword(); 
String s = new String(pwd);  //converting char to string
String p = s;

//In the JButton checking username and password(Username check works fine but not passwordfield)

jb.addActionListener(new ActionListener() {
                         public void actionPerformed(ActionEvent e)
                           {
                            if ((jt.getText().length()) == 0)
                            {
                                   JFrame jf1 = new JFrame("UserName");
                                         jf1.setSize(401, 401);
                                         //jf1.setVisible(true);
                                         jf1.setDefaultCloseOperation(jf1.EXIT_ON_CLOSE);
                                         JOptionPane.showMessageDialog(jf1, "User Name is empty");
                            }

                         else if((p.length()) == 0)
                            {

                                   JFrame jf1 = new JFrame("Password");
                                         jf1.setSize(401, 401);
                                         //jf1.setVisible(true);
                                         jf1.setDefaultCloseOperation(jf1.EXIT_ON_CLOSE);
                                         JOptionPane.showMessageDialog(jf1, "Password is empty");
                            }

                         else if ((p.length() == 0) && (jt.getText().length()) == 0)
                            {
                                   JFrame jf1 = new JFrame("UserName and Password");
                                         jf1.setSize(401, 401);
                                         //jf1.setVisible(true);
                                         jf1.setDefaultCloseOperation(jf1.EXIT_ON_CLOSE);
                                         JOptionPane.showMessageDialog(jf1, "Username and Password is 
empty");
                            }

                            else
                            { //go to another method}
Can someone help
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
user1815823
  • 617
  • 2
  • 8
  • 14

1 Answers1

12

Very simple get the text using JPasswordField#getPassword() which returns a char[] of the text, then simply get the length of the array and check if it is equal to 0:

JPasswordField jpf...

if(jpf.getPassword().length == 0) {
    // it is empty
}else {
   // it is not empty
}
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • 1
    Thanks David. it worked. but why not length() == 0 but .length ==0? – user1815823 Nov 19 '12 at 20:03
  • 2
    @user1815823 Because it is an Array, which has a variable called `length` which gives us the length /number of items in the array. While String does not have a variable called `length` it has a method called `length()` which computes the Strings length hope that helps. Also please do click the tick next to the persons answer who helped you to show gratituity – David Kroukamp Nov 19 '12 at 20:08