JavaFX 分页,添加<<和>>>选项



我正在做一个数据显示应用程序,我需要在我的分页中添加一个选项,可用于返回第一页(索引)或转到最后一页。

我已经尝试向我的 UI 添加按钮,但它不起作用,因为我无法获取最后一个索引。

@FXML
void goToLastIndex(ActionEvent event) {
    int lastIndex = pagination.getPageCount();
    pagination.setCurrentPageIndex(lastIndex);
}

你有没有研究过分页的页面计数属性。

/**
* Returns the number of pages.
*/
public final int getPageCount() { return pageCount.get(); }
/**
 * The number of pages for this pagination control.  This
 * value must be greater than or equal to 1. {@link #INDETERMINATE}
 * should be used as the page count if the total number of pages is unknown.
 *
 * The default is an {@link #INDETERMINATE} number of pages.
 */
public final IntegerProperty pageCountProperty() { return pageCount; }

根据Javadocs,pageCount的默认值是Pagination.INDETERMINATE,它(或多或少是任意的)等于Integer.MAX_VALUE。如果您的分页有固定的页数(如果没有,则拥有"最后一页"实际上没有意义),那么您应该通过调用采用页计数值的构造函数来初始化它,或者调用setPageCount(...)并指定页数。

谢谢,我需要做的就是创建一个保持页数的变量,并将其与setOnAction一起使用。

int numberOfPage = (nbOfDataForCurrentType / ROW_PER_PAGE + 1);
        pagination = new Pagination(numberOfPage, 0);
        pagination.setPageFactory(param -> populateTableView(param));
        getChildren().add(pagination);
        if (numberOfPage > 1) {
            btnEnd.onActionProperty().set(event -> pagination.setCurrentPageIndex(numberOfPage));
            btnBegin.setOnAction(event -> pagination.setCurrentPageIndex(0));
            getChildren().add(btnBegin);
            getChildren().add(btnEnd);
        }
    });

最新更新