Java:如何更新自定义ListCellRenderer?
我为我的聊天应用程序实现了自己的 ListCellRenderer。我使用 JList 列出所有用户。单元格渲染器主要由一个图标组成,该图标显示特定用户当前是否在线或离线以及他/她的姓名。该列表由 DefaultListModel 控制,我用它为 JList 提供必要的信息。
但是当列表模型确实改变其状态时(例如用户离线),列表单元格渲染器似乎没有被调用?
有人知道如何解决这个问题吗?尝试在 JList 实例上调用 updateUI(),但没有帮助。
非常感谢!
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 (e.g. 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
单元格渲染器可能工作正常。不起作用的是 ListModel。
DefaultListModel
不会检测模型对象内部状态的更改。您需要在列表模型上调用fireContentsChanged
。可能您需要向模型对象添加侦听器,甚至可能需要扩展DefaultListModel
;因为我没有看到它的代码,所以我不知道你的代码是什么样的。您不应该只调用一个听起来不错的随机方法(
updateUI
做了一些非常不同的事情)。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 callfireContentsChanged
on the list model. Probably you need to add listeners to your model objects and maybe you even have to extend theDefaultListModel
; 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).