简体   繁体   中英

How to tell the JList cellrenderer for sudden JList changes?

I have a JList that will point to a selected JList depending on user. It acts like a monitor that will monitor the selected 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. How do I notify the JList to reevaluate its list data without having to invoke add/remove function?

From the comments underneath the question I gathered you have one list A which should show the contents of another list. 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'.

You can share the ListModel behind the JList instances. 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. 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.

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:

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

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