Ok, I am working a Java Swing JPanel where I am doing a database DELETE from a table. I have a JList control that is populated with the names of profiles from the database. I query the database for a list of Profiles that I then keep in an ArrayList. I copy the Profile Name from each ArrayList entries and add it to the DefaultListModel for the JList. My Panel has a "Delete" button that issues a DefaultListModel.remove() call and then issues the DELETE SQL statement to the Database. Everything works ok until I get to the last Profile that I want to delete. I get a message that the database delete went fine, but then I get the following IndexOutOfBoundsException:
My Panel Class:
From looking at the Stack Trace it looks like my DefaultListModel.remove() call (ProfilePanel.btnActnActionPerformed(ProfilePanel.java:626) is causing the valueChanged() method in listSelectListener to be fired (listSelectListener.valueChanged(ProfilePanel.java:886)). This causes my listener to try and locate an entry in what is an empty list, thus the IndexOutOfBoundsException. But I don't get why the listener is being executed because I deleted an Item from the list? I suppose I could remove the listSelectListener before I remove the list Entry but I am not sure this is a good idea. If I have "if listModel.size() > 0" in valueChanged method, then the Delete works Ok, but I can't add a New Profile because the same ListModel is being used for adding profiles. I am pretty sure I am doing something wrong, but I am not sure where I am going wrong.