简体   繁体   English

从JList删除项目

[英]Removing an item from a JList

I know this question has been asked before, and there are lots of threads where I could cut and paste working code, but my question is about why the code I have below doesn't work. 我知道以前曾问过这个问题,并且有很多线程可以剪切和粘贴工作代码,但是我的问题是为什么下面的代码无法正常工作。 I am trying to remove multiple items from a JList, but when I run the following code I get an out of bounds exception. 我试图从JList中删除多个项目,但是当我运行以下代码时,出现了超出范围的异常。 Here's the snippit: 这是代码段:

static DefaultListModel<File> listModel = new DefaultListModel<>();
JList<File> fileList = new JList<>(listModel);

void removeFiles() {
    int[] listRange = new int[100];
    listRange = fileList.getSelectedIndices();
    int i = 0;
    while (i <= listRange.length) {
        listModel.remove(listRange[i]);
        i++;
    }
}

I've used debug statements to confirm that fileList is getting the data (ie if I add 4 files its length is 4), and I've also confirmed that the indices in listRange represent the indexes of the files which I want to remove. 我已经使用调试语句来确认fileList正在获取数据(即,如果我添加4个文件,其长度为4),并且我还确认了listRange的索引代表了我要删除的文件的索引。 But for some reason, it won't remove them. 但是由于某种原因,它不会删除它们。 I've tried removing from fileList , as well as from the model ( listModel ), but neither will work. 我尝试从fileList以及模型( listModel )中listModel ,但是都listModel正常工作。 Am I overlooking something here? 我在这里俯瞰什么吗? Thanks. 谢谢。

When you remove an item from the list, its size will decrease. 当您从列表中删除一个项目时,其大小将减小。

So for example in a list of 3 items, you want to remove item at index 1 and 2. When you remove the first one, the list has only 2 items remaining at index 0 and 1. So calling list.remove(2) will result in an outOfBoundException 因此,例如,在3个项目的列表中,您要删除索引1和2的项目。删除第一个项目时,列表中只有2个项目保留在索引0和1。所以调用list.remove(2)将导致outOfBoundException

A possible solution is to use the iterator and keep calling next until you reach one of the indices you want to remove and you call remove on it. 一种可能的解决方案是使用迭代器并继续调用下一个,直到达到要删除的索引之一,然后对其调用remove。 Or simply decrease the next index to remove by the number of removal already performed 或者只是将下一个要删除的索引减少已执行的删除次数

PS: this only works if getSelectedIndices returns an ordered array. PS:仅当getSelectedIndices返回有序数组时,此方法才有效。 Otherwise, you have to order the indices yourself 否则,您必须自己订购索引

static DefaultListModel<File> listModel = new DefaultListModel<>();
JList<File> fileList = new JList<>(listModel);

void removeFiles() {
    int[] listRange = new int[100];
    listRange = fileList.getSelectedIndices();
    int i = 0;
    //The counter of the removed items so far
    int nbOfRemovedItems = 0;

    while (i <= listRange.length) {
        // After each remove, the index of the items is decreased by one
        listModel.remove(listRange[i] - nbOfRemovedItems);
        ++nbOfRemovedItems;
        i++;
    }
}

The out of the box solution would be to remove items in reverse order as to avoid the out of bounds exception: 开箱即用的解决方案是按照相反的顺序删除项目,以避免出现越界异常:

int[] listRange = fileList.getSelectedIndices();
int i = listRange.length-1;
while (i >= 0) {
    listModel.remove(listRange[i]);
    i--;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM