Question

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:

  • Jlist is already instantiated with some elements.
  • I need to remove all the elements from the list.
  • I do not have the JList object(Component) accessible in the model, so can't call repaint().

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

Any suggestions?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top