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());

>

[4417 byte] By [KalEl6355a] at [2007-10-2 18:41:46]
# 1

Hi,

You don't need to create a new list every time. Just do the add and delete on the defaultlistmodel. Any change you make automatically fires an event that asks the list to redraw itself. Refer the javadoc of JList to see how to access its model. Usually this is done as follows:

DefaultListModel defModel = new DefaultListModel();

for(int i=0;i<100;i++)

defModel.addRow(i, String.valueOf(i));

}

mylist.setModel(defModel);

Also your listrenderer extends JLabel and implements ListRenderer. Instead you can have your istrenderer etend DefaultListCellRenderer which is a kind of JLabel.

Hope this helps.

cheers,

vidyut

http://www.bonanzasoft.com

vidyuta at 2007-7-13 20:03:58 > top of Java-index,Desktop,Core GUI APIs...
# 2

The Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/list.html]How to Use Lists[/url] has a working example of dynically updating a list.

If you just need to rerender an item because its data changes then you would use the fireContentsChanged(...) method of the DefaultListModel.

camickra at 2007-7-13 20:03:58 > top of Java-index,Desktop,Core GUI APIs...
# 3
Thanks guys, I'm new to java and wasn't aware of the DefaultListModel class. I'll read up on it. Thanks again.- Leon
KalEl6355a at 2007-7-13 20:03:58 > top of Java-index,Desktop,Core GUI APIs...