I have this class:
@SuppressWarnings("serial")
private class DataCellRenderer extends JLabel implements ListCellRenderer
{
public DataCellRenderer()
{
setHorizontalAlignment(SwingConstants.RIGHT);
}
@Override
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
if(isSelected)
setBackground(Color.red);
setText(" " + value.toString());
return this;
}
}
The problem is that my Background will not turn red when I select a cell in my JList. The setText part works but I can't figure out why it will not change my background color of the cell. Anyone have any ideas, Thanks!
setOpaque(true);
in there, I think that's why the OP's renderer was not behaving correctly. – Steere