JList and ListCellRenderer question
Hi all,
I have a newbie question for anyone willing to answer. I'm making a JList using a ListCellRenderer where each item in the list holds a little image icon and a user name. After the list is added to a JScroll panel and displayed, it often needs to get updated. Very frequently the image icon will need to change in some list items and less frequently list items will need to be added and removed. Is there a way to modify the list without creating a new one and applying the ListCellRenderer I made everytime I need to change an icon or add/remove a user? Here is a small piece of my code if it helps explain at all what exacly I'm trying to do. Any help would be greatly appreciated. Thanks.
class UserListItemimplements MyStatConstants
{
privatefinal String _user;
privatefinal String _statusIconPath;
public UserListItem(String user, String statusIcon)
{
_user = user;
if (statusIcon.equals(_AT_DESK))
_statusIconPath ="Images/greenDot.PNG";
elseif (statusIcon.equals(_OUT_FOR_LUNCH))
_statusIconPath ="Images/blueDot.PNG";
elseif (statusIcon.equals(_OFFLINE))
_statusIconPath ="Images/redDot.PNG";
else
_statusIconPath ="Images/yellowDot.PNG";
}
public String getUser()
{return _user;}
public ImageIcon getImage()
{returnnew ImageIcon(getClass().getResource(_statusIconPath));}
}
class UserListCellRendererextends JLabelimplements MyStatConstants, ListCellRenderer
{
public UserListCellRenderer()
{
setOpaque(true);
setIconTextGap(10);
}
public Component getListCellRendererComponent(JList list, Object value,
int index,boolean isSelected,boolean cellHasFocus)
{
UserListItem item = (UserListItem) value;
setText(item.getUser());
setIcon(item.getImage());
if (isSelected)
{
setBackground(HIGHLIGHT_COLOR);
setForeground(WHITE_COLOR);
}
else
{
setBackground(WHITE_COLOR);
setForeground(DARK_GRAY_COLOR);
}
returnthis;
}
}
// This code is somewhere else
_itemList =new UserListItem[employees.length];
for(int i=0; i<employees.length; i++)
_itemList[i] =new UserListItem(employees[i], statusMsgs[i]);
_userList =new JList(_itemList);
_userList.setVisibleRowCount(15);
_userList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
_userList.setCellRenderer(new UserListCellRenderer());
>

