Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle pagination with page factory

I have a table view with an anchor pane panel with 2 children, a table view and a pagination. The pagination is not directly linked to the table view (like if you put a button with a label that gets updated).

The only examples I found is that the pagination itself handles the UI updates via it's setPageFactory method.

I know I shouldn't design it like this, unfortunately I don't have the time to change it for now. So here's my current solution:

paginationTab1.setPageFactory(e -> {
    updateTableViewWithOffset(e);
    //hack, as the pagination is not directly linked with the tableView
    //just return an empty component that is not managed by the parent component
    Label l = new Label();
    l.setManaged(false);
    return l;
});

Is this an acceptable workaround (return null doesn't update well the UI after...) ? Or is there a way to get the same listener's behavior as the setPageFactory method provides (i.e get the page offset when it's clicked either on the pagination's arrows or pagination's numbers)?

like image 608
user2336315 Avatar asked Sep 12 '25 22:09

user2336315


1 Answers

You can observe the Pagination's currentPageIndexProperty():

paginationTab1.currentPageIndexProperty().addListener((obs, oldIndex, newIndex) -> 
    updateTableViewWithOffset(newIndex.intValue()));

Here's a SSCCE:

import java.util.stream.Collectors;
import java.util.stream.IntStream;

import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class IndependentPaginationTest extends Application {

    private static final int NUM_PAGES = 20 ;
    private static final int ITEMS_PER_PAGE = 20 ;

    @Override
    public void start(Stage primaryStage) {
        TableView<String> table = new TableView<>();
        TableColumn<String, String> col = new TableColumn<>("Item");
        table.getColumns().add(col);

        col.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue()));
        updateTable(table, 0);

        Pagination pagination = new Pagination();
        pagination.setPageCount(NUM_PAGES);

        pagination.currentPageIndexProperty().addListener((obs, oldIndex, newIndex) -> 
                updateTable(table, newIndex.intValue()));

        BorderPane root = new BorderPane(table, null, null, pagination, null);
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }

    private void updateTable(TableView<String> table, Integer index) {
        int start = index * ITEMS_PER_PAGE + 1;
        int end = start + ITEMS_PER_PAGE ;
        table.getItems().setAll(
                IntStream.range(start, end)
                .mapToObj(Integer::toString)
                .map("Item "::concat)
                .collect(Collectors.toList()));
    }

    public static void main(String[] args) {
        launch(args);
    }
}
like image 54
James_D Avatar answered Sep 15 '25 14:09

James_D