File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes IndexOutOfBoundsException Deleting Last Item from JList Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "IndexOutOfBoundsException Deleting Last Item from JList " Watch "IndexOutOfBoundsException Deleting Last Item from JList " New topic
Author

IndexOutOfBoundsException Deleting Last Item from JList

Rw Adams
Greenhorn

Joined: Oct 28, 2011
Posts: 10
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
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8562

Put in a check for ListSelectionEvent#getValueIsAdjusting to avoid getting false positives on selection change.


[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
unsure if this will work, but worth a try



Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8562

Michael Dunn wrote:


I would rather
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
when reading code I often overlook the !
so prefer to have it stand out as == false
Rw Adams
Greenhorn

Joined: Oct 28, 2011
Posts: 10
Michael & Maneesh Thanks for the suggestions but I still get the error....


Any other ideas appreciated, I am still stumped....
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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()
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: IndexOutOfBoundsException Deleting Last Item from JList
 
Similar Threads
Jtable and database results
Remove comma in the JSpinner
Interesting error with CipherInputStream
How to convert arabic numbers received from JTextField to a Double?
nullPointerException on entity manager methods