如何删除JList中的多个项目 [英] How to remove multiple items in JList

查看:71
本文介绍了如何删除JList中的多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很有趣,我无法找到如何删除JList中多个选定项的方法

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

请帮助

UPD: 好的,问题出在NetBeans中,因为它创建了JList并设置了AbstractListModel模型,该模型在某种程度上无法与remove方法一起使用.

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

推荐答案

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

这篇关于如何删除JList中的多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆