S
Scott Steiner
Hi,
I need a JList that displays items which consist of an image and some
text. For this purpose I wrote my own ListCellRenderer which did the
trick. However, I'm now having a problem with the selection colors. When
an item is selected then the text disappears and only the image is
visible. If I don't use MyListCellRenderer then everything is fine and
the selected item gets highlighted in the windows default blue, but as
soon as MyListCellRenderer is set as the default cell renderer then no
highlighting is there.
Any help appreciated.
public class MyListCellRenderer extends JLabel implements
ListCellRenderer
{
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
if (isSelected)
{
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}
else
{
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setText(value.toString());
setIcon(new ImageIcon(value));
return this;
}
}
I need a JList that displays items which consist of an image and some
text. For this purpose I wrote my own ListCellRenderer which did the
trick. However, I'm now having a problem with the selection colors. When
an item is selected then the text disappears and only the image is
visible. If I don't use MyListCellRenderer then everything is fine and
the selected item gets highlighted in the windows default blue, but as
soon as MyListCellRenderer is set as the default cell renderer then no
highlighting is there.
Any help appreciated.
public class MyListCellRenderer extends JLabel implements
ListCellRenderer
{
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
if (isSelected)
{
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}
else
{
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setText(value.toString());
setIcon(new ImageIcon(value));
return this;
}
}