• Post Reply Bookmark Topic Watch Topic
  • New Topic

IndexOutOfBoundsException Deleting Last Item from JList  RSS feed

 
Rw Adams
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks for the help!
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put in a check for ListSelectionEvent#getValueIsAdjusting to avoid getting false positives on selection change.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
unsure if this will work, but worth a try



 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:


I would rather
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when reading code I often overlook the !
so prefer to have it stand out as == false
 
Rw Adams
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael & Maneesh Thanks for the suggestions but I still get the error....


Any other ideas appreciated, I am still stumped....
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
leave the getValueIsAdjusting() in the code and try your original fix "if listModel.size() > 0",
and see if there's still any side effects from add()
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!