Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
JavaFX TableView with Pagination example
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.scene.Node;
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;
import java.util.ArrayList;
import java.util.List;
public class TestTablePagination extends Application {
private final static int dataSize = 10_023;
private final static int rowsPerPage = 1000;
private final TableView<Sample> table = createTable();
private final List<Sample> data = createData();
private List<Sample> createData() {
List<Sample> data = new ArrayList<>(dataSize);
for (int i = 0; i < dataSize; i++) {
data.add(new Sample(i, "foo " + i, "bar " + i));
return data;
private TableView<Sample> createTable() {
TableView<Sample> table = new TableView<>();
TableColumn<Sample, Integer> column1 = new TableColumn<>("Id");
column1.setCellValueFactory(param -> param.getValue().id);
TableColumn<Sample, String> column2 = new TableColumn<>("Foo");
column2.setCellValueFactory(param -> param.getValue().foo);
TableColumn<Sample, String> column3 = new TableColumn<>("Bar");
column3.setCellValueFactory(param -> param.getValue().bar);
table.getColumns().addAll(column1, column2, column3);
return 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);
public void start(final Stage stage) throws Exception {
Pagination pagination = new Pagination((data.size() / rowsPerPage + 1), 0);
Scene scene = new Scene(new BorderPane(pagination), 1024, 768);
stage.setTitle("Table pager");;
public static void main(String[] args) throws Exception {
public static class Sample {
private final ObservableValue<Integer> id;
private final SimpleStringProperty foo;
private final SimpleStringProperty bar;
private Sample(int id, String foo, String bar) { = new SimpleObjectProperty<>(id); = new SimpleStringProperty(foo); = new SimpleStringProperty(bar);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.