Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPasswordField KeyPress string length error?

I am trying to change background colors of a JPasswordField in Java Swing (Netbeans).

Here's what I have:

private void pstxtPasswordKeyPressed(java.awt.event.KeyEvent evt) {                                         

    //Get string from password box
    userPassword = new String(pstxtPassword.getPassword());

    //If password is 8+ characters
    //(one less because string counting begins at 0)
    if (userPassword.length() >= 7) {

        //Set password input box background color to green
        pstxtPassword.setBackground(Color.green);
    }

    else { //If password is less than 8 characters

        //Set password input box background color to red
        pstxtPassword.setBackground(Color.red);
    }

}

Everything works, except when I backspace. When I backspace after typing 8+ characters, the color doesn't change back to red until there is only 5 characters left in the field.

Help would be appreciated, I am very new to java programming and Netbeans.

EDIT: I have changed my code,

    //If password is 8+ characters
    if ((pstxtPassword.getPassword()).length >= 8) {

        //Set password input box background color to green
        pstxtPassword.setBackground(Color.green);
    }

    else { //If password is less than 8 characters

        //Set password input box background color to red
        pstxtPassword.setBackground(Color.red);
    }

This code seems to make sense to me, but in testing, the color changes green at the 9th character; when backspacing, it changes back to red at 6. This seems to be the same problem I had when the code was >= 7 where the color changed green at the 8th character, but changed back to red at 5 characters.

After typing 9 characters, the component turns green

After typing 9 characters, the component turns green

After backspacing (starting from 9), component remains green until there are 6 characters

After backspacing (starting from 9), component remains green until there are 6 characters

This is strange, because I have similar code in a button in this program which displays an error message. That code works fine. Is this a KeyPress problem, perhaps something to do with the backspace key?

like image 397
jessechk Avatar asked Oct 19 '12 02:10

jessechk


People also ask

What is JPasswordField in Java?

JPasswordField is a lightweight component that allows the editing of a single line of text where the view indicates something was typed, but does not show the original characters. You can find further information and examples in How to Use Text Fields, a section in The Java Tutorial.

Which method is used to change the default character of JPasswordField?

The argument passed into the JPasswordField constructor indicates the preferred size of the field, which is at least 10 columns wide in this case. By default a password field displays a dot for each character typed. If you want to change the echo character, call the setEchoChar method.


Video Answer


2 Answers

As an aside, examine the length of the array returned by getPassword(), rather than the length of a String constructed from that array. The String is a security risk, as it will be stored for an indefinite time with the easily found name userPassword.

Addendum: Here's a related example of Robin's suggestion to use DocumentListener. I'm guessing that your key listener is seeing the KeyEvent before the JPasswordField has processed it.

like image 107
trashgod Avatar answered Oct 05 '22 12:10

trashgod


Since JPasswordField extends from JTextComponent, you can attach a DocumentListener to it which is a far safer manner to update the background color on each change of the contents.

like image 37
Robin Avatar answered Oct 05 '22 10:10

Robin