JavaFX Pagination, adding << and>>> options

2019-09-17 20:25发布

问题:

I am doing a data displaying app and I need to add a option to my pagination which can be used to go back to the first page (index) or go to the last one.

I already tried adding buttons to my UI but it did not work because I could not obtain the last index.

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

回答1:

Have you looked into the pageCountProperty of the pagination.

/**
* 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; }


回答2:

According to the Javadocs the default value of pageCount is Pagination.INDETERMINATE, which is (more or less arbitrarily) equal to Integer.MAX_VALUE. If your pagination has a fixed number of pages (if not, it doesn't really make sense to have a "last page"), then you should initialize it by calling the constructor taking a page count value, or call setPageCount(...) and specify the number of pages.



回答3:

Thank you, all I needed to do was to create a variable that keeps the number of page and use it with a 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);
        }

    });