简体   繁体   English

Java:如何更新自定义ListCellRenderer?

[英]Java: How to update custom ListCellRenderer?

I implemented my own ListCellRenderer for my chat app. 我为聊天应用程序实现了自己的ListCellRenderer。 I use a JList to list all users. 我使用JList列出所有用户。 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. 该列表由DefaultListModel控制,我用它为JList提供必要的信息。

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. 试图在JList实例上调用updateUI(),但没有帮助。

Many thanks in advance! 提前谢谢了!

The cell renderer probably works fine. 单元格渲染器可能工作正常。 What is not working is the ListModel. 不起作用的是ListModel。 The DefaultListModel does not detect changes to the internal state of the model objects. DefaultListModel不会检测对模型对象内部状态的更改。 You need to call fireContentsChanged on the list model. 您需要在列表模型上调用fireContentsChanged Probably you need to add listeners to your model objects and maybe you even have to extend the DefaultListModel ; 可能您需要向模型对象添加侦听器,甚至可能必须扩展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). 您不仅应该使用听起来不错的名称来调用随机方法( updateUI功能大不相同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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