davidahines
davidahines

Reputation: 4094

Forcing the update of a JList

When I edit the quantity of an object in a list and the toString changes, how do I get the list to update with the new toString?

Example, when I change nodes in my JTree, I use nodeChanged(node) what do I do when I change a list item?

Upvotes: 0

Views: 3860

Answers (2)

davidahines
davidahines

Reputation: 4094

List.updateUI() will do it, although I'm told this has some overhead.

Upvotes: 0

Robin
Robin

Reputation: 36611

AbstractListModel#fireContentsChanged if you extend from AbstractListModel.

It is the same principle as for the JTree of your previous question. The AbstractListModel does not know when some internal property of your objects is changed. So when you make a change, you must call that particular method indicating the object is changed. The list model will fire the correct event which is received by the JList, which will update/repaint (whatever you want to call it).

Personally I prefer to create ListModels which are self-contained, e.g. if the objects you use fire events when they change the ListModel can listen for those changes and fire the appropriate events itself instead of having to call this method externally.

Edit

Just noticed that that particular method is protected, so you need to create the model as I suggested and you cannot call that method externally (which is a good thing)

Upvotes: 5

Related Questions