I need to implement color choices using JComboBox. Wrote this renderer:
private class ColorCellRenderer implements ListCellRenderer { protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer(); public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { JLabel renderer = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if (value instanceof Color) { renderer.setBackground((Color) value); renderer.setText(" "); } return renderer; } }
The problem is that when you select an item from the JComboBox list, the background color changes for the entire JComboBox (including the button), and not just for its text field.