How to remove multiple items in JList

VextoR picture VextoR · Oct 6, 2011 · Viewed 8.8k times · Source

It's funny, I can't find out how to delete multiple selected items in a JList

Help please

enter image description here

UPD: OK, the problem was in NetBeans, because it creates JList and sets model AbstractListModel which somehow not working with remove method.

Answer

VextoR picture VextoR · Oct 6, 2011
   DefaultListModel dlm = (DefaultListModel) subjectList.getModel();

      if(this.subjectList.getSelectedIndices().length > 0) {
          int[] selectedIndices = subjectList.getSelectedIndices();
          for (int i = selectedIndices.length-1; i >=0; i--) {
              dlm.removeElementAt(selectedIndices[i]);
          } 
    }