Pagination

Auto advance

Pagination p = new Pagination(10);

Timeline fiveSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(5), event -> {
    int pos = (p.getCurrentPageIndex()+1) % p.getPageCount();
    p.setCurrentPageIndex(pos);
}));
fiveSecondsWonder.setCycleCount(Timeline.INDEFINITE);
fiveSecondsWonder.play();

stage.setScene(new Scene(p));
stage.show();

This advances the pagination every 5 seconds.

How it works

Pagination p = new Pagination(10);

Timeline fiveSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(5), event -> {

fiveSecondsWonder is a timeline that fires an event every time it finishes a cycle. In this case the cycle time is 5 seconds.

    int pos = (p.getCurrentPageIndex()+1) % p.getPageCount();
    p.setCurrentPageIndex(pos);

Tick the pagination.

}));
fiveSecondsWonder.setCycleCount(Timeline.INDEFINITE);

Set the timeline to run forever.

fiveSecondsWonder.play();

Create a pagination of images

ArrayList<String> images = new ArrayList<>();
images.add("some\\cool\\image");
images.add("some\\other\\cool\\image");
images.add("some\\cooler\\image");

Pagination p = new Pagination(3);
p.setPageFactory(n -> new ImageView(images.get(n)));

Note that the paths must be urls, not filesystem paths.

How it works

p.setPageFactory(n -> new ImageView(images.get(n)));

Everything else is just fluff, this is where the real work is happening. setPageFactory takes a callback that takes an int, and returns the node that we want at that index. The first page maps to the first item in the list, the second to the second item in the list and so on.

Creating a pagination

Paginations in JavaFX use a callback to get the pages used in the animation.

Pagination p = new Pagination();
p.setPageFactory(param -> new Button(param.toString()));

This creates an infinite list of buttons numbed 0.. since the zero arg constructor creates an infinite pagination. setPageFactory takes a callback that takes an int, and returns the node that we want at that index.



2016-08-04
2016-08-04
javafx Pedia
Icon