Java : JList 只查找可见范围内的索引

Java : JList only finds indices in visible range

我在单击按钮时执行以下代码:

while(count<listAmount){
        tbiCList.setSelectedIndex(0);

        String item = JList.getSelectedValue();

            String update[] = item.split(", ");

            //this part removed for sake of post

        remove(item);
        count++;
    }

备注:

count 以 0 开始,listAmountJList 中的项目数量,而 remove() 是我有的一种删除所选项目的方法。

这一切工作正常而且花花公子,但是,问题是,它只选择和删除单击按钮时可见的项目,而不是滚动窗格后的项目。当然,我可以再次单击该按钮,并获得下一组项目,但是,我是一名程序员,我想要 最简单 的方式来完成我的任务。

更新

我意识到我正在通过获取 LastVisibleIndex();

来设置 listAmount

我的错。

我现在的问题:如何在每次 while 命令更新时检索 listAmount

更新更新

因为我没有耐心,所以我经常这样做,所以,我很抱歉。

我想出了解决问题的方法。在 while 循环中,我刚刚将 +1 添加到 listAmount 变量,然后当它给了我 NullPointerException 我在 while 块周围添加了一个 try 块.就是这样,问题已解决。

与其使用 getSelectedValue() 获取单个 selection,不如删除 while 语句并使用 getSelectedValuesList() 代替 select 多个项目,然后遍历这些项目。

不要忘记查看 JavaDoc 以了解其他选项。

编辑评论:

List<String> selectedItems = myJList.getSelectedValuesList();
for (int i = 0; i < selectedItems.size(); i++){
    String item = selectedItems.get(i);
    String update[] = item.split(", ");
    //this part removed for sake of post
    //....
}
myJList.removeAll();