Search code examples
javaswingnetbeansjlist

How to remove multiple items in JList


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.


Solution

  •    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]);
              } 
        }