I have a simple slide program, and left side of my application, there is a JList which shows the slides on the current application. I can save and load the slides. The problem is, when I’m trying to load the slides from XML file, I cannot delete all items, in the JList, and add them. Because when I remove elements by model.removeAllElements(); and then trying to add items by;
for(int i=0; i<mL.size(); i++){
model.add(i, "Slide No: " + i);
slideCounter++;
}
Then valueChanged function will be called, and because I’m getting elements from arrayList in that function, it gives ArrayIndexOutOfBoundsException
Therefore, in my load method, I create a new empty list(dMode), then I initialize the list with the number of slides by:
list = new JList(dMode);
jScrollPane1 = new JScrollPane(list);
but I cant assign new list to the current list.
What is your advice, how should I solve this ?
Thanks.
I would either implement my own ListModel or I would bind the data to the list using JGoodies Binding. When using JGoodies your view model could fire a
PropertyChangeEventwhenever your array contents changes and your view will then be updated automatically. E.g.with a view model class that has a property for the list contents and one for the current selection.