在JavaFX中搜索TableView列表 [英] Search TableView list in JavaFX

查看:94
本文介绍了在JavaFX中搜索TableView列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在TableWie中查找记录(例如,通过ID)并选择已创建的Row并将其放入Java 8(JavaFX)屏幕的中间.

How to find a record in TableWie (for example via ID) and select founded Row and put it to the middle of the screen in Java 8 (JavaFX ).

推荐答案

您可以使用类似以下内容的元素来搜索元素:

You can search for an element using something like:

int searchId = ... ;

table.getItems().stream().filter(item -> item.getId()==searchId).findAny()

然后只需使用通常的方法来选择项目(如果有的话). TableView具有scrollTo(...)方法,该方法将滚动以使该项目可见:

Then just use the usual methods to select the item (if there is one). The TableView has a scrollTo(...) method that will scroll so the item is visible:

table.getItems().stream()
    .filter(item -> item.getId() == searchId)
    .findAny()
    .ifPresent(item -> {
        table.getSelectionModel().select(item);
        table.scrollTo(item);
    });

如果表中有大量项目,则可能需要实现更有效的搜索算法(例如,按ID排序并像@Bartek的答案一样使用二进制搜索).

If you have a very large number of items in the table, you may need to implement a more efficient search algorithm (e.g. sort by id and use a binary search as in @Bartek's answer).

这篇关于在JavaFX中搜索TableView列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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