problem with JList (remove element from list)

I have a Vector in static class.

It keeps values.

I have a JList. It represents vector values:

I load vector to JList this way:

publicvoid loadjListHistoryModel(){

DefaultListModel dlm =new DefaultListModel();

for(int i=0;i<LoadDriver.gethistoryChangesVector().size();i++){

dlm.addElement(LoadDriver.gethistoryChangesVector().get(i));

}

jListHistory.setModel(dlm);

this.pack();

}

The problem is:

User chooses value from JList and deletes it.

It user does this operation the first time, Everything is O.K.

If he does it the second time, I get

java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0

at java.awt.Container.remove(Container.java:1132)

I delete element this way:

privatevoid jButton1ActionPerformed(java.awt.event.ActionEvent evt){

int selIndex = jListHistory.getSelectedIndex();//J List selected index

LoadDriver.deleteElementFromhistoryChangesVector(selIndex);//deletes selected value from vector

System.out.println(" got element " + jListHistory.getModel().getElementAt(selIndex) );//try to get element to delete - OK. I get no error, I get my element

jListHistory.remove(selIndex);//ERROR

loadjListHistoryModel();//reload JList model (Vector has been changed)

}

The first time, it works. Element could be deleted without any problem.

The second time I get error in marked code line.

What does it mean?

Why I can get element, which I want to delete, but I can't delete it....

I can't understand - where is mistake!>

[2253 byte] By [Holoda] at [2007-11-27 6:17:47]
# 1

Hi Holod,

You should not remove elements from a JList using remove function of JList.

You should remove the element from the model.

Read the tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/components/list.html#mutable

Anyway, since you reload the model you can just remove this line:

jListHistory.remove(selIndex);//ERROR

Rodney_McKaya at 2007-7-12 17:31:05 > top of Java-index,Desktop,Core GUI APIs...
# 2

[code]

LoadDriver.deleteElementFromhistoryChangesVector(selIndex); //remove element from vector

((DefaultListModel) jListHistory.getModel()).remove(selIndex);//remove element from model

loadjListHistoryModel();//reload model/code]

I did it this way!

Thank you. Everything works!!!

Ofcource, you are right, I do not even need to delete element from model, because I reload each time JList model.

[code]

LoadDriver.deleteElementFromhistoryChangesVector(selIndex); //remove element from vector

loadjListHistoryModel();//reload model/code]

Holoda at 2007-7-12 17:31:05 > top of Java-index,Desktop,Core GUI APIs...