File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes problems index after adding element to a Jlist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problems index after adding element to a Jlist" Watch "problems index after adding element to a Jlist" New topic

problems index after adding element to a Jlist

Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
I can finally add a new element to a Jlist.
Everything works out fine when I add a new element without selecting something first.
When I have selected something from the Jlist and try to add a new item I get the following errors on the outputscreen :
Connection established successfully!
rowssize = 7
newIndex Jlist = 6
rowssize = 8
Exception occurred during event dispatching:
java.lang.ArrayIndexOutOfBoundsException: -1 < 0
at java.util.Vector.elementAt(
at veld.VeldGUI.veldenListSelected1(
at veld.VeldGUI.access$300
Here is the code for the actionHandler when I add an new element,FYI I work with vectors and not the DefaultListModel.

// Here I put the index of the Jlist to the added element
int newIndex = (rows.size()-1);
System.out.println ("newIndex Jlist = " +
Here is my ListSelectionListener when an item is selected :
private void veldenListSelected1(javax.swing.event.ListSelectionEvent evt) {
tfLengte.setText(new Integer(((Veld)rows.elementAt(veldenList.getSelectedIndex())).getLengte()).toString());

I agree. Here's the link:
subject: problems index after adding element to a Jlist
jQuery in Action, 3rd edition