The renderer appears to work for the list items, but for some reason,
the selection item (the "trigger" button on the JComboBox) doesn't
appear to use the renderer.
I have tried setting a custom editor (using the setEditor() method),
without any change in behavior. It is my understanding that the
editor is only used when setEditable(true) is set. This is not the
case with my example.
I've pared down my code to a minimum to demonstrate the problem. I've
tried this with Java 1.1.8/Swing 1.1.1 and JDK 1.2.2 (both on Linux).
Can somebody tell me what I am doing wrong?
import java.awt.*;
import javax.swing.*;
public class Test
extends JPanel
{
private JComboBox combo = null;
private final static String myText = " ";
private static Object[] colors = {
Color.black, Color.green, Color.red,
Color.blue, Color.lightGray, Color.white,
Color.cyan, Color.magenta, Color.yellow,
Color.darkGray, Color.orange,
Color.gray, Color.pink,
};
static class ColorCellRenderer
implements ListCellRenderer
{
private JLabel renderer;
public ColorCellRenderer() {
renderer = new JLabel();
renderer.setOpaque(true);
renderer.setText(myText);
}
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
if (value != null)
renderer.setBackground((Color) value);
return renderer;
}
}
public Test() {
super(new BorderLayout());
combo = new JComboBox(colors);
combo.setRenderer(new ColorCellRenderer());
combo.setSelectedIndex(0);
add(combo, BorderLayout.CENTER);
}
public static void main(String args[]) {
Test c = new Test();
JFrame f = new JFrame("ColorComboBox Test");
f.setContentPane(c);
f.pack();
f.show();
}
}
--
lar3ry gensch lar...@mediaone.net
"Y2038 Glitch... Here we come!"