JavaFX

如何使用JavaFX构建一个具有分页和全局排序的TabbleVi

2018-08-06  本文已影响210人  Huangjs1994

JavaFX提供了TableView用来显示数据,但是TableView没有分页功能,并且在加上分页功能后,TableViewjinjin只能在一页里面排序。
完整示例见:https://github.com/lhuangjs/blog/tree/master/src/main/java/javafx/tableview

学英语 ing,注释使用英文,如有不当,请指教

第一步:创建带有分页功能的TableView

1. 首先我们新建一个Page类,用来管理数据

import javafx.beans.property.SimpleIntegerProperty;

import java.util.List;

public class Page<T> {
    private SimpleIntegerProperty totalRecord; // total record number in source data
    private SimpleIntegerProperty pageSize; // the number of data in per page
    private SimpleIntegerProperty totalPage; // total page number
    private List<T> rowDataList; // total data

    /** setter **
    /** getter **/
 
   /**
     * @param rowDataList
     * @param pageSize    the number of data in per page
     */
    public Page(List<T> rowDataList, int pageSize) {
        this.totalRecord = new SimpleIntegerProperty();
        this.totalPage = new SimpleIntegerProperty();
        this.rowDataList = rowDataList;
        this.pageSize = new SimpleIntegerProperty(pageSize);
        initialize();


    }

    private void initialize() {
        totalRecord.set(rowDataList.size());

        // calculate the number of total pages
        totalPage.set(
                totalRecord.get() % pageSize.get() == 0 ?
                        totalRecord.get() / pageSize.get() :
                        totalRecord.get() / pageSize.get() + 1);

        // add listener: the number of total pages need to be change if the page size changed
        pageSize.addListener((observable, oldVal, newVal) ->
                totalPage.set(
                        totalRecord.get() % pageSize.get() == 0 ?
                                totalRecord.get() / pageSize.get() :
                                totalRecord.get() / pageSize.get() + 1)
        );
    }

    /**
     * current page number(0-based system)
     *
     * @param currentPage current page number
     * @return
     */
    public List<T> getCurrentPageDataList(int currentPage) {
        int fromIndex = pageSize.get() * currentPage;
        int tmp = pageSize.get() * currentPage + pageSize.get() - 1;
        int endIndex = tmp >= totalRecord.get() ? totalRecord.get() - 1 : tmp;

        // subList(fromIndex, toIndex) -> [fromIndex, toIndex)
        return rowDataList.subList(fromIndex, endIndex + 1);
    }
}
        pageSize.addListener((observable, oldVal, newVal) ->
                totalPage.set(
                        totalRecord.get() % pageSize.get() == 0 ?
                                totalRecord.get() / pageSize.get() :
                                totalRecord.get() / pageSize.get() + 1)
        );

在程序中,为变量pageSize添加了一个监听器,如果pageSize的值改变,那么总页数(totalPage)也需要随之改变。

2. 添加分页功能到TableView

import javafx.collections.FXCollections;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableView;

public class TableWithPaginationAndSorting<T> {
    private Page<T> page;
    private TableView<T> tableView;
    private Pagination tableViewWithPaginationPane;
    
    /** getter **/

    public TableWithPaginationAndSorting(Page<T> page, TableView<T> tableView) {
        this.page = page;
        this.tableView = tableView;
        tableViewWithPaginationPane = new Pagination();
        tableViewWithPaginationPane.pageCountProperty().bindBidirectional(page.totalPageProperty());
        updatePagination();
    }

    private void updatePagination() {
        tableViewWithPaginationPane.setPageFactory(pageIndex -> {
            tableView.setItems(FXCollections.observableList(page.getCurrentPageDataList(pageIndex)));
            return tableView;
        });
    }
}

关于page factory,可以将它想象成一个加工厂,它负责根据提供的页码生产对应的页面,所以,你可以根据不同的页码显示不同的内容。并且这里我们不用每次都新建一个表格,只需要每次将数据添加到建好了的表格框架

image.png
    private void updatePagination() {
        tableViewWithPaginationPane.setPageFactory(new Callback<Integer, Node>() {

            @Override
            public Node call(Integer pageIndex) {
                tableView.setItems(FXCollections.observableList(page.getCurrentPageDataList(pageIndex)));
                return tableView;
            }
        });
    }

3. 建立一个测试类

我们先建立一个测试类来测试我们已经开发的功能。

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.util.Arrays;
import java.util.List;

public class TableWithPaginationAndSortingTest extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        // create table
        TableView<People> peopleTable = createTable();

        // get data
        List<People> peopleList = getTableData();
        peopleTable.setItems(FXCollections.observableList(peopleList));

        // create Page object
        Page<People> page = new Page<>(peopleList, 2);

        // add pagination into table
        TableWithPaginationAndSorting<People> table = new TableWithPaginationAndSorting<>(page, peopleTable);

        Scene scene = new Scene(new BorderPane(table.getTableViewWithPaginationPane()), 300, 300);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private TableView<People> createTable() {
        TableView<People> table = new TableView<>();
        TableColumn<People, String> nameCol = new TableColumn<>("name");
        nameCol.setCellValueFactory(new PropertyValueFactory("name"));
        TableColumn<People, Integer> ageCol = new TableColumn<>("age");
        ageCol.setCellValueFactory(new PropertyValueFactory("age"));
        table.getColumns().addAll(nameCol, ageCol);
        table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
        return table;
    }

    private List<People> getTableData() {
        return Arrays.asList(new People[]{
                        new People("Huang1", 18),
                        new People("Huang2", 11),
                        new People("Huang3", 13),
                        new People("Huang4", 28),
                        new People("Huang5", 38)
                }
        );
    }

    public class People {
        private String name;
        private int age;

        public People(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读