Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ListCellRenderer wont update until selected

843806May 10 2007 — edited May 11 2007
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()); 
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 8 2007
Added on May 10 2007
2 comments
165 views