Reputation: 4169
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?
Upvotes: 0
Views: 554
Reputation: 691685
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);
Upvotes: 2
Reputation: 36601
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() );
}
Upvotes: 3