i have a ComboBox named 'ComboBox_status' which have default values 'In Operation' and 'Disposed'
i want the background color to change to red when 'Disposed' is selected and green when 'In Operation' is selected.

this is the code i tried but it changes the background to red for both of them when they are selected

String age="Disposed";

        if("Disposed".equals(age)){
                  ComboBox_status.setBackground(Color.red); 
                  ComboBox_status.setForeground(Color.BLACK);
                }
                else{
                    ComboBox_status.setBackground(Color.GREEN);
                    ComboBox_status.setForeground(Color.darkGray);

                }  

i will appreciate it if anyone could help me resolve this THANK YOU.

Dani AI

Generated

The problem in the original post by is that a fixed string was being tested instead of the JComboBox's actual selection, so the same branch is always executed. already pointed out the hardcoding issue and correctly suggested using a renderer — that is the most reliable way in Swing to control how the selected value (and the list items) are painted.

A concise, robust approach is to install a custom ListCellRenderer that sets background/foreground based on the item text. The renderer paints both the drop-down cells and the display area, and avoids look-and-feel quirks that can make direct calls to setBackground on the JComboBox ineffective.

comboBox.setRenderer(new DefaultListCellRenderer() {
    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        setOpaque(true);
        String s = value == null ? "" : value.toString();
        if ("Disposed".equals(s)) {
            setBackground(Color.RED);
            setForeground(Color.BLACK);
        } else if ("In Operation".equals(s)) {
            setBackground(Color.GREEN);
            setForeground(Color.DARK_GRAY);
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }
        return this;
    }
});

Notes and quick troubleshooting:

  • For editable JComboBox instances, the editor component must be colored separately via comboBox.getEditor().getEditorComponent().
  • If colors still do not appear, ensure the renderer component is opaque (setOpaque(true)) and call comboBox.repaint().
  • An alternative is to attach an ItemListener/ActionListener and change the combo box/editor background on selection, but many LAFs ignore setBackground on the combo itself — the renderer approach is more consistent.

Recommended Answers

All 4 Replies

how do you expect this to return false:

String age="Disposed";
if("Disposed".equals(age)){

i know i need help
am new@java

basically: what you are doing is:

if ( true ) 
  setColor(RED);
else
  setColor(BLACK);

and for some reason, you expect the else block to be executed. don't hardcode the value of age, get the value selected in your JComboBox

btw job for Renderer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.