I have a window with a JListBox and a button which deletes all the elements of the list except the element that is currently selected on the JListBox.
So, this is the code I've written at an unsuccessful attempt:
I'm actually just confused of what to do in order to make this happen, I'm trying anythin.
The strange thing is that there is always going to be an ArrayIndexOutOfBoundsException and after that listBox's Model gets modified without me trying to change it, I've only tried to changed defaultListModel.
Try iterating backwards, starting at defaultListModel.getSize() and continuing as long as i >= 0. The thing is, if you remove the element at index X, then the element previously at index X + 1 gets placed at index X. The loop doesn't know about that though. By iterating backwards, you will shift elements back, but you've already encountered those elements and will no longer examine them.