This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes ListSelectionlistener problem when adding new elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ListSelectionlistener problem when adding new elements" Watch "ListSelectionlistener problem when adding new elements" New topic
Author

ListSelectionlistener problem when adding new elements

Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
Hello ,
Subject says it all.
I received help to clear the Jlist selection !
thanks Manfred !!
But I still get an exception in the ListSelectionListener when I add a new element to the Jlist.
The problem I'm having is situated in the ListSelectionListener where I set the elements which are inside a vector to the selected item in the Jlist.
This works veryw well EXCEPT when I add a new element to the Jlist.
When adding a new element I receive an exception like this :
Exception occurred during event dispatching:
java.lang.ArrayIndexOutOfBoundsException: -1 < 0
at java.util.Vector.elementAt(Vector.java:427)
at veld.VeldGUI.veldenListSelected1(VeldGUI.java:236)
at veld.VeldGUI.access$300(VeldGUI.java:23)
at veld.VeldGUI$4.valueChanged(VeldGUI.java:128)
at javax.swing.JList.fireSelectionValueChanged(JList.java:946)
at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:960)
etc.....
I thought with clearing the selection in the list I could solve this but it doesn't work.
I probably have to do something in the listener.
Does anybody have any advice to solve this problem?
Should I work with a Datalistener ?
Thanks,
Kristof
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Kristof,
Not exactly clear on what you are doing, but this might or might not help you. The ListSelectionListener actually gets called twice for each user selection. You should use the method isValueAdjusting to make sure you only run your code once and not twice. That might actually be your problem (i.e., the double call could be screwing you up). Example:

Regards,
Manfred.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ListSelectionlistener problem when adding new elements
 
Similar Threads
How to clear the selection made in a Jlist
problems index after adding element to a Jlist
JList Selection
Adding elements to a JList dynamically
Remove JList