setBackground in ComboBoxes

T

Thomas Richter

Hi,

is there a known problem with custom renderers in JComboBoxes?

I've here the following problem:

Given a class derived from the swing JComboBox with a custom
renderer given as follows

class ColorComboBoxRenderer implements ListCellRenderer {
/* snip */
public Component getListCellRendererComponent(JList list,
Object value,int index,
boolean isSelected,
boolean cellHasFocus) {
ColorLabel label = (ColorLabel)value;
//
if (isSelected) {
label.setBackground(list.getSelectionBackground());
label.setForeground(list.getSelectionForeground());
} else {
label.setBackground(list.getBackground());
label.setForeground(list.getForeground());
}
// We should maybe render the selected item in a nicer way...
return label;
}

and a ColorLabel class that is directly derived from the JLabel, I get
no visible feedback at all if the user moves the pointer over the elements
of the ComboBox. However, I can make sure (by a couple of "println"'s) that
the code arrives at the right spot (above) and that the colors feed into
the label class are, as such, correct.

Any idea what's going on here? Is it impossible to change the background
color of a JLabel in a custom renderer of a JComboBox?

Greetings,
Thomas
 
V

VisionSet

Any idea what's going on here? Is it impossible to change the background
color of a JLabel in a custom renderer of a JComboBox?

In a rush, so haven't read your post properly.
IIRC JLabels are transparent by default and inherit the background of their
parent, setOpaque(true) and it will work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top