Deleting from a List
In this code, when I select an element from the middle of the list and delete, the elements below the selected element are also removed from "view". But they are present in the database and appear once again when the app is run. Please help me with this mistake. Thanks.
DeleteController delController = new DeleteController();
delController.deleteInfo(dbId);
this.jList1 = list;
AbstractListModel model = (AbstractListModel) jList1.getModel();
int numberElements = model.getSize();
final String[] allElements = new String[numberElements + 1];
for (int i = 0; i < numberElements - 1; i++) {
String val = (String) model.getElementAt(i);
if (!dbId.equals(val)) {
allElements[i] = (String) model.getElementAt(i);
}
}
jList1.setModel(new javax.swing.Abstrac开发者_高级运维tListModel() {
String[] strings = allElements;
public int getSize() {
return strings.length;
}
public Object getElementAt(int i) {
return strings[i];
}
});
Use DefaultListModel
. It has removeElementAt()
method
精彩评论