How to register a change in a JList Model?

2.7k Views Asked by At

I am having issues understanding which interface I need to listen to changes in a listModel. I thought it was ListDataListener, but I can't understand the methods in it. There are 3 methods in it and this one seems to be the one I want but I can't understand the description:

contentsChanged(ListDataEvent e) 
      Sent when the contents of the list has changed in a way that's too complex to characterize with the previous methods.

What does it mean by "too complex"? And by "characterize with the previous methods"? Well, what does the whole thing mean? Is this the interface I want?

2

There are 2 best solutions below

1
On BEST ANSWER

Yes, that's the right listener.

The statement means that this method is the more general one, that will cover every possible change to the list content. The other ones (intervalAdded and intervalRemoved) should be used when those specific events occur.

In my practice you will always use the most general one (even with table listeners).. I guess it was supposed to be used to optimize (especially with big lists).

A tutorial on this listener can be found here.

0
On

ListModel dispatches events to its ListDataListener listeners. It's more efficient for the list model to invoke the detailed intervalAdded and intervalRemoved methods when possible. The Listener of the list model (in this case a JList) can use these detailed changes to make minimal changes to the visual component (ie, for intervalAdded it can just add the new row instead of redrawing the whole list).

However, some changes may be too complex to be described as with just added and removed. In this case, the list model has the option of invoking contentsChanged. When JList sees contentsChanged it will most likely refetch the entire list from the list model.