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 8845177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:40:33+00:00 2026-06-14T11:40:33+00:00

I have a difficult problem with this code: public class StatusAndPersonPanel implements ActionListener {

  • 0

I have a difficult problem with this code:

public class StatusAndPersonPanel implements ActionListener {

    private JPanel myPanel;
    private String[] status = {"Good", "Bad", "Not so bad"};
    private Color[] backgrounds = {Color.GREEN, Color.RED, Color.ORANGE};
    private JComboBox<String> box;

    public ComboboxConstructor() {
        myPanel = new JPanel();
        box = new JComboBox<String>(status);
        ComboBoxRenderer myBackgrounds = new ComboBoxRenderer(box);
        myBackgrounds.setColors(backgrounds);
        myBackgrounds.setStrings(status);

        box.setRenderer(myBackgrounds);
        box.addActionListener(this);

        Color selectedColor = (Color) backgrounds[box.getSelectedIndex()];
        box.setBackground(selectedColor);
        myPanel.add(box); // putting it into my panel
    }

    @Override
    public void actionPerformed(ActionEvent event) {

        Color selectedColor = (Color) backgrounds[box.getSelectedIndex()];
        box.setBackground(selectedColor);
    }
}

As you can see in the image, in Step 1 all colors are made as I want them,
but now I click on “not as bad” and the JComboBox collapses with the right choice, but doesn´t change the color (the arrow does, I don´t know why), until my focus is lost (Step 3, clicking another `JPanel element) the color is given properly.

Now, I know it´s just a little cosmetic problem, but it´s driving me crazy! I tried it with ActionListener, ItemListener (after change), and FocusListener (focus lost and gained), but the result stays always the same as shown in Step 2.

image

Thank you so much for your help and thoughts in advance!

  • 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-14T11:40:35+00:00Added an answer on June 14, 2026 at 11:40 am

    A JComboBox is displayed using a single renderer for all cells. You can change the color in your ListCellRenderer, as shown in the CustomComboBoxDemo.

    Addendum: Related examples may be found in this Q&A.

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

Sidebar

Related Questions

I have a problem with a difficult regex. I have this expression to detect
I have a problem that is kind of difficult to explain. What I would
I'm not sure how difficult this but I have an array and would like
I have tried making a fiddle for this, but it's been too difficult to
I have some code like this: final int height = 0; //I want to
I have a service that implements the interface LocationListener with the methods of this
Back story: I have a class that handles a database query. The problem is
I'm having a bit of difficulty with some java code class foo{ public bar()
Imagine I have a class in C# called Bar that has a public function
I'm finding it difficult to describe this problem very concisely, so I've attached the

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.