从 JavaFX 2.0 中的 TableView 读取多项选择

发布于 2024-12-19 21:01:58 字数 409 浏览 4 评论 0原文

我试图从 JavaFX 2.0 中的 TableView 中获取选择。我在 TableView 中存储了 5 个人(5 行)。获取选择模型的代码是:

TableView<Person> tableView =
myStage.getTableView();

ObservableList<Person> selection = 
tableView.getSelectionModel().getSelectedItems();

System.out.println(selection.size());

现在,当我选择多行,然后执行包含上述代码的方法时,以下内容会打印选择 * 2,有时会打印选择 * 3。例如:我选择所有 5 行,它会打印尺寸为 10,有时为 15!

我在这里做错了什么?

I am trying to get the selection from a TableView in JavaFX 2.0. I have stored 5 Persons (5 rows) in the TableView. The code to get the selection model is:

TableView<Person> tableView =
myStage.getTableView();

ObservableList<Person> selection = 
tableView.getSelectionModel().getSelectedItems();

System.out.println(selection.size());

Now when I select multiple rows and then execute the method including the above code, the following prints the selection * 2 and sometimes the selection * 3. For example: I select all 5 rows and it prints out a size of 10 and sometimes 15!

What am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

執念 2024-12-26 21:01:58

TableView 存在一个错误,它会返回重复的项目以供通过 Shift 键单击进行选择。
作为修复之前的解决方法,您可以尝试通过以下方式过滤重复的项目:

Set<Person> selection = new HashSet<Person>(tableView.getSelectionModel().getSelectedItems());

There is a bug with TableView returning duplicated items for selection made by shift-click.
As a workaround until fix you can try filter duplicated items by:

Set<Person> selection = new HashSet<Person>(tableView.getSelectionModel().getSelectedItems());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文