简体   繁体   English

如何告诉JList cellrenderer JList突然更改?

[英]How to tell the JList cellrenderer for sudden JList changes?

I have a JList that will point to a selected JList depending on user. 我有一个JList,它将根据用户指向选定的JList。 It acts like a monitor that will monitor the selected JList. 它就像一个监视器,它将监视选定的JList。 When I implements custom cell renderer and programmically do something like this.list = getSelectedList() , the cell renderer does not react to this sudden change of information. 当我实现自定义单元格渲染器并以编程方式执行类似this.list = getSelectedList() ,单元格渲染器不会对信息的这种突然变化做出反应。 How do I notify the JList to reevaluate its list data without having to invoke add/remove function? 如何通知JList重新评估其列表数据,而不必调用添加/删除功能?

From the comments underneath the question I gathered you have one list A which should show the contents of another list. 从我收集的问题下面的注释中,您可以看到一个列表A,其中应显示另一个列表的内容。 The A list should show the contents of a list selected by the user, and you have a problem when the user changes the 'selected list'. A列表应显示用户选择的列表的内容,当用户更改“选择的列表”时,您会遇到问题。

You can share the ListModel behind the JList instances. 您可以在JList实例后面共享ListModel So you could have something like 所以你可能会喜欢

public void selectionChanged( JList selectedList ){
  //update the model of this.list to match the model of selectedList
  this.list.setModel( selectedList.getModel() );
}

this.list = getSelectedList() affects another JList object to the this.list field. this.list = getSelectedList()影响this.list字段的另一个JList对象。 If you set a custom renderer to this.list before executing this line, you set the renderer on another JList object, and there is no way that the renderer becomes magically attached to the new selected list. 如果在执行此行之前将自定义渲染器设置为this.list ,则将渲染器设置在另一个JList对象上,并且无法将渲染器神奇地附加到新的选定列表。

You're confusing variables and objects. 您在混淆变量和对象。 When you call a method on the object, you're modifying the object the variable points to, and not the variable itself. 在对象上调用方法时,是在修改变量指向的对象,而不是变量本身。 If you want to attach the same cell renderer to the newly selected JList, you need the following code: 如果要将同一单元格渲染器附加到新选择的JList,则需要以下代码:

ListCellRenderer renderer = this.list.getCellRenderer();
this.list = getSelectedList();
this.list.setCellRenderer(renderer);

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

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