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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:21:05+00:00 2026-06-05T15:21:05+00:00

I’m trying to make a ComboBox that uses different colors for different items. I

  • 0

I’m trying to make a ComboBox that uses different colors for different items. I wrote out some test code but it doesn’t seem to work. Adding in the renderer causes the program to crash but commenting it out makes the box display on in the frame.

Is there anything I’m missing or am I doing this the wrong way? I tried using the custom ComboBox Renderer tutorial as an example.

Here is my code:

TestComboColor.java

import java.awt.Color;

import javax.swing.JComboBox;
import javax.swing.JFrame;


public class TestComboColor {

    static Color[] colors = {Color.BLUE, Color.GRAY, Color.RED};
    static String[] strings = {"Test1", "Test2", "Test3"};

    public static void main(String[] args)
    {
        JFrame frame = new JFrame("JAVA");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JComboBox cmb = new JComboBox();
        ComboBoxRenderer renderer = new ComboBoxRenderer(cmb);

        renderer.setColors(colors);
        renderer.setStrings(strings);

        cmb.setRenderer(renderer);

        frame.add(cmb);
        frame.pack();
        frame.setVisible(true);
    }
}

ComboBoxRenderer.java

import java.awt.Color;
import java.awt.Component;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;


public final class ComboBoxRenderer extends JPanel implements ListCellRenderer
{

    private static final long serialVersionUID = -1L;
    private Color[] colors;
    private String[] strings;

    JPanel textPanel;
    JLabel text;

    public ComboBoxRenderer(JComboBox combo) {

        textPanel = new JPanel();
        textPanel.add(this);
        text = new JLabel();
        text.setOpaque(true);
        text.setFont(combo.getFont());
        textPanel.add(text);
    }

    public void setColors(Color[] col)
    {
        colors = col;
    }

    public void setStrings(String[] str)
    {
        strings = str;
    }

    public Color[] getColors()
    {
        return colors;
    }

    public String[] getStrings()
    {
        return strings;
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {

        if (isSelected)
        {
            setBackground(list.getSelectionBackground());
        }
        else
        {
        }

        if (colors.length != strings.length)
        {
            System.out.println("colors.length does not equal strings.length");
            return this;
        }
        else if (colors == null)
        {
            System.out.println("use setColors first.");
            return this;
        }
        else if (strings == null)
        {
            System.out.println("use setStrings first.");
            return this;
        }

        text.setText(strings[index]);
        text.setForeground(colors[index]);
        text.setBackground(getBackground());
        return text;


    }

}

Thanks!

  • 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-05T15:21:06+00:00Added an answer on June 5, 2026 at 3:21 pm

    Is this what you mean?

    TestComboColor

    import java.awt.Color;
    import java.awt.Component;
    import javax.swing.*;
    
    public class TestComboColor {
    
        static Color[] colors = {Color.BLUE, Color.GRAY, Color.RED};
        static String[] strings = {"Test1", "Test2", "Test3"};
    
        public static void main(String[] args)
        {
            JFrame frame = new JFrame("JAVA");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JComboBox cmb = new JComboBox(strings);
            ComboBoxRenderer renderer = new ComboBoxRenderer(cmb);
    
            renderer.setColors(colors);
            renderer.setStrings(strings);
    
            cmb.setRenderer(renderer);
    
            frame.add(cmb);
            frame.pack();
            frame.setVisible(true);
        }
    }
    
    class ComboBoxRenderer extends JPanel implements ListCellRenderer
    {
    
        private static final long serialVersionUID = -1L;
        private Color[] colors;
        private String[] strings;
    
        JPanel textPanel;
        JLabel text;
    
        public ComboBoxRenderer(JComboBox combo) {
    
            textPanel = new JPanel();
            textPanel.add(this);
            text = new JLabel();
            text.setOpaque(true);
            text.setFont(combo.getFont());
            textPanel.add(text);
        }
    
        public void setColors(Color[] col)
        {
            colors = col;
        }
    
        public void setStrings(String[] str)
        {
            strings = str;
        }
    
        public Color[] getColors()
        {
            return colors;
        }
    
        public String[] getStrings()
        {
            return strings;
        }
    
        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
    
            if (isSelected)
            {
                setBackground(list.getSelectionBackground());
            }
            else
            {
                setBackground(Color.WHITE);
            }
    
            if (colors.length != strings.length)
            {
                System.out.println("colors.length does not equal strings.length");
                return this;
            }
            else if (colors == null)
            {
                System.out.println("use setColors first.");
                return this;
            }
            else if (strings == null)
            {
                System.out.println("use setStrings first.");
                return this;
            }
    
            text.setBackground(getBackground());
    
            text.setText(value.toString());
            if (index>-1) {
                text.setForeground(colors[index]);
            }
            return text;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.