Hi,
I wonder if anyone could help, I have been implementing a ListCellRenderer, and have just changed it to inherit DefaultListCellRenderer. However, either way the JList will only update when an (or any for that matter) element is selected in it. The minute you click the JList though, it updates just as it should. Here's the code
/* Custom cell renderer for the JList */
class MyCellRenderer extends DefaultListCellRenderer {
public ImageIcon onlineIcn, offlineIcn;
public MyCellRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
ImageIcon onlineIcn = new ImageIcon("icons/status_online.png");
ImageIcon offlineIcn = new ImageIcon("icons/status_offline.png");
String s = value.toString();
JLabel moo = new JLabel();
contact con = (contact) value;
moo.setText(con.getName());
moo.setIcon(con.getStatus().equals("online") ? onlineIcn : offlineIcn);
moo.setBackground(isSelected ? Color.lightGray : Color.white);
moo.setForeground(con.getStatus().equals("online") ? Color.green : Color.red);
return moo;
}
}
Oh and here is the JList being called:
model = new DefaultListModel();
list = new JList(model);
list.setCellRenderer(new MyCellRenderer());