Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JComboBox + Custom Renderer not quite working

0 views
Skip to first unread message

lar3ry gensch

unread,
Feb 23, 2000, 3:00:00 AM2/23/00
to
I am trying to create a JComboBox that will display a list of colors,
each as a JLabel with the appropriate background. I have a custom
renderer to display the colors.

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!"

0 new messages