Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Can't render background color in JComboBox, after selection.

834336Apr 12 2012 — edited Nov 4 2013
I don't understand why this problem is so difficult. It is driving me nuts.

If I create a renderer for a Jcombobox, it will correctly render TEXT and COLOR if it is displaying it in the pop up list, but as soon as it has been selected, the combo will render TEXT correctly but will ignore COLOR. I have tried playing around with opacity and with the editors, but it just seems as if JComboBox renderers do not work 100% as expected.

Here is my text code. All I want is for the color of X or Y to be reflected in the selected element :
 


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

import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

public class CustomComboBoxDemo extends JPanel {
 
    public CustomComboBoxDemo() {
    	
        super(new BorderLayout());
 
        String[] s = { "1", "2", "3", "4", "5" };
        
        JComboBox c = new JComboBox(s);
        JComboBox c2 = new JComboBox(s);
        
        c.setOpaque(true);
        
        ComboBoxRenderer renderer= new ComboBoxRenderer();
        c.setRenderer(renderer);
        c2.setRenderer(renderer);
 
        JPanel p = new JPanel();
        p.add(c);
        p.add(c2);
        
        add(p);
    }

 
    class ComboBoxRenderer extends DefaultListCellRenderer {
 
        public ComboBoxRenderer() {
            setOpaque(true);
        }

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

        	JLabel c = (JLabel)super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        	
        	if (value.toString().equals("3")) {
            	c.setText("X");
            	c.setBackground(Color.RED);
        	} else {
            	c.setText("Y");
            	c.setBackground(Color.GREEN);
        	}
        	
        	return this;
        }
    }
    
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
    
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("CustomComboBoxDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new CustomComboBoxDemo());
 
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 2 2013
Added on Apr 12 2012
9 comments
8,869 views