Mo3z
Mo3z

Reputation: 2128

Update JList after removing all the elements from the model(AbstractListModel)

I have a JList where each element holds some special data. So I have implemented AbstractListModel for the JList.

Everything works great when I have one or more elements in the list.

But, does not work in the scenario below:

I cannot call fire***() methods of AbstractListModel since it throws OutOfIndexException.

Any suggestions?

Upvotes: 1

Views: 860

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

You state:

Because I am calling fireContentsChanged(this, 0,0)..which will look for 0th element in the list which does not exist. I know the cause but not the solution.

Don't call fireContentsChanged(...) when removing items. Call fireIntervalRemoved(...) instead. Also as an aside, you should never have to call repaint() in this situation. Changing the state of the model and then notifying the model's listeners through a call to the proper fireXXX(...) method is all that should be done.

Upvotes: 3

Related Questions