Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8706689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:40:31+00:00 2026-06-13T03:40:31+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:40:32+00:00Added an answer on June 13, 2026 at 3:40 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the background colors of the strips to change (it is
Hey i am trying to change the background color of a div. I have
Can't change the body background for some reason, here is how I am trying
Using JQuery's jquery.animate-colors-min.js plugin I am trying to change the background color of some
Hello I am trying to change the colors of my swing progress bar in
I am trying to change the Background Color of the ParentGrid when the child
I am trying to change the menuOption background-color on hover using JQuery. All my
I'm trying to change background-size using css: I want 100 heights and 100 widths,
I am trying to change the background image of a link when it get
I'm trying to change background color of one of my UIView subclasses. For some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.