Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

java - How to check if JPassword field is null

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...