MyListCellRenderer: no highlighting anymore when item is selected

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;
}
}
 
T

Tom N

Scott said:
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.

Have you checked what the colours are that you are getting from list.getSelectionBackground() and
list.getSelectionForeground()?

It might be worth hard-coding some colours for testing purposes as the list might be supplying colours which are
the same for FG and BG.

The DefaultListCellRenderer doesn't do much different from your code except at the start it does...
setComponentOrientation(list.getComponentOrientation());
and at the end it does...
setBorder((cellHasFocus) ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);

and it does this in the constructor...
noFocusBorder = new EmptyBorder(1, 1, 1, 1);
setOpaque(true);
setBorder(noFocusBorder);

I can't see any of that affecting colours except perhaps the setOpaque().

It might be easier to subclass DefaultListCellRenderer and write the method (assuming value is an Icon) as...

public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus);
setText(value.toString());
}
 
T

Tom N

Tom said:
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
super.getListCellRendererComponent( list, value, index,
isSelected, cellHasFocus); setText(value.toString()); return this;
}
 
S

Scott Steiner

Tom N wrote:
[...]
It might be easier to subclass DefaultListCellRenderer and write the method (assuming value is an Icon) as...

public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus);
setText(value.toString());
}
[...]

Thanks Tom! Subclassing DefaultListCellRenderer and calling
super.getListCellRendererComponent was the solution to my problem.
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top