简体   繁体   中英

Java: How to update custom ListCellRenderer?

I implemented my own ListCellRenderer for my chat app. I use a JList to list all users. The cell renderer consists mainly of an icon that displays if a particular user is currently on- or offline and his/her name. The list is controlled by a DefaultListModel which I use to provide the JList with the necessary information.

But when the list model does change its state (eg a user goes offline), the list cell renderer seems not to be invoked?

Somebody any idea how to solve this problem? Tried to call updateUI() on the JList instance, but did not helped.

Many thanks in advance!

The cell renderer probably works fine. What is not working is the ListModel. The DefaultListModel does not detect changes to the internal state of the model objects. You need to call fireContentsChanged on the list model. Probably you need to add listeners to your model objects and maybe you even have to extend the DefaultListModel ; as I don't see the code of it I don't know how yours look.

You should not just call a random method with a name that sounds good ( updateUI does something very different).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM