如何在JavaFX中实现TableView的分页? [英] How to implement paging for a TableView in JavaFX?

查看:2358
本文介绍了如何在JavaFX中实现TableView的分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JavaFx TableView,并希望实现tableView分页显示。
如何使用javaFx tableView实现分页显示?

解决方案

你必须使用分页控制并实现页面工厂。为每个应显示的页面调用工厂,您可以使用其参数pageIndex为TableView提供项目的子列表:

  TableView table = ... 

private Node createPage(int pageIndex){

int fromIndex = pageIndex * rowsPerPage;
int toIndex = Math.min(fromIndex + rowsPerPage,data.size());
table.setItems(FXCollections.observableArrayList(data.subList(fromIndex,toIndex)));

返回新的BorderPane(表);
}


@Override
public void start(最后阶段阶段)抛出异常{

分页分页=新分页((数据) .size()/ rowsPerPage + 1),0);
pagination.setPageFactory(this :: createPage);
...
}

可在此处找到完整的可运行示例:
https://gist.github.com/timbuethe/7becdc4556225e7c5b7b



PS:我关注了jewelsea链接到Oracle JavaFX论坛,示例非常糟糕。我想在这里提供一个清理过的例子。


I have JavaFx TableView, and want to implement tableView paging display. How to javaFx tableView Implement paging display?

解决方案

You have to use the Pagination control and implement a page factory. The factory is called for every page that should be displayed and you can use its parameter, the pageIndex, to provide a sublist of items to the TableView:

TableView table = ...

private Node createPage(int pageIndex) {

    int fromIndex = pageIndex * rowsPerPage;
    int toIndex = Math.min(fromIndex + rowsPerPage, data.size());
    table.setItems(FXCollections.observableArrayList(data.subList(fromIndex, toIndex)));

    return new BorderPane(table);
}


@Override
public void start(final Stage stage) throws Exception {

    Pagination pagination = new Pagination((data.size() / rowsPerPage + 1), 0);
    pagination.setPageFactory(this::createPage);
    ...
}

A complete runnable example can be found here: https://gist.github.com/timbuethe/7becdc4556225e7c5b7b

PS: I followed jewelsea link to the Oracle JavaFX Forums, and the example is really bad. I wanted to provide an cleaned up example here.

这篇关于如何在JavaFX中实现TableView的分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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