JavaFX:清除ListView [英] JavaFX: Clearing the ListView

查看:631
本文介绍了JavaFX:清除ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在点击某个按钮时清除ListView中的所有内容。我试图通过索引删除它,但它给了我例外。我没有完全得到ListView的SelectionModel。以下是我的尝试:

I want to clear all content from the ListView when I click on some button. I was trying to remove it by indices but it was giving me exceptions. I don't quite get the SelectionModel of ListView. Here are my attempts:

asiLogsListView.getSelectionModel().selectAll();
        ObservableList<Integer> indices = asiLogsListView.getSelectionModel().getSelectedIndices();
        for(int index : indices) {
            asiLogsListView.getSelectionModel().getSelectedItems().remove(index);
        }

asiLogsListView.getSelectionModel().getSelectedItems().removeAll(indices);


推荐答案

清除<$ c $中的所有商品c> ListView ,只需执行

asiLogsListView.getItems().clear();

如果要清除选择,请执行

If you want to clear the selection, then do

asiLogsListView.getSelectionModel().clearSelection();

棘手的是从 ListView中删除所有选定的项目

List<Integer> selectedItemsCopy = new ArrayList<>(asiLogsListView.getSelectionModel().getSelectedItems());
asiLogsListView.getItems().removeAll(selectedItemsCopy);

您的代码看起来正在尝试清除选择,因为您试图删除所有元素来自 selectionModel selectedItems 列表。问题是,当您删除每个项目时,其余项目的索引会发生变化,因此您最终会删除错误的项目,并可能最终导致 ArrayIndexOutOfBoundsException s(如果您的项目数量少于所选项目的最大索引数。)

Your code looks like it is trying to clear the selection, because you are trying to remove all the elements from the selectionModel's selectedItems list. The problem is that as you remove each item, the index of the remaining items would change, so you end up removing the wrong items, and potentially may end up with ArrayIndexOutOfBoundsExceptions (if you end up with fewer items than the largest index of a selected item).

这篇关于JavaFX:清除ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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