JPasswordField KeyPress Line Length Error?

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

Here is 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 fell behind. When I return after entering 8 + characters, the color will not change to red until there are only 5 characters left in the field.

Help will be appreciated, I am very new to programming in Java and Netbeans.

EDIT: I 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 when testing, the color changes to green at the 9th character; when moving backwards, it changes to red by 6. This seems to be the same problem as mine when the code was >= 7 , where the color changed green to the eighth character but changed to red with 5 characters.

After typing 9 characters, the component turns green

After entering 9 characters, the component will turn green

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

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

This is strange because I have a similar code in a button in this program that displays an error message. This code is working fine. Is this a KeyPress problem, maybe something is related to the backspace key?

+6
source share
5 answers

Note the length of the array returned by getPassword() , not the length of the String constructed from this array. String represents a security risk, as it will be stored indefinitely with the easily found name userPassword .

Appendix: Here is an example Robin program for using DocumentListener . I assume your key listener sees a KeyEvent before JPasswordField processes it.

+8
source

Since JPasswordField continues from JTextComponent , you can attach a DocumentListener to it, which is much safer to update the background color every time the content changes.

+8
source
 if (userPassword.length() >= 7) 

This if statement does not match your comments:

// If the password has 8+ characters

The actual code says that if there are 7+ characters, then set the background color to green. Therefore, when you go back, it should rotate the red background when up to 6 characters remain.

I think your confusion is shown in this comment:

 //(one less because string counting begins at 0) 

What you are trying to describe is that indexing a character in a String starts at 0 , for example when you use charAt() or subString() . This means that the first character has index 0 , the second has index 1 , etc. length() , on the other hand, returns the number of characters in a String . This has nothing to do with indexing, so you do not need to subtract 1.

+7
source

I solved the problem using KeyRelease instead of KeyPress, try my friend

0
source

Using

 private void pstxtPasswordKeyReleased(java.awt.event.KeyEvent evt) 

Instead

 private void pstxtPasswordKeyPressed(java.awt.event.KeyEvent evt) 
0
source

Source: https://habr.com/ru/post/928083/


All Articles