JavaFX Pagination

0

JavaFX Pagination : Pagination Control

Pagination Control is used for navigation between pages of a content.

pagination control consists of the page content and the page navigation areas.

Constructors :

  • Pagination()Constructs a Pagination control with an Indeterminate page count and a page index equal to zero.
  • Pagination(int pageCount)Constructs a new Pagination control with the specified page count.
  • Pagination(int pageCount, int pageIndex)Constructs a new Pagination control with the specified page count and page index.

Creating Pagination Control :

Pagination pagination1 = new Pagination();

Or

Pagination pagination1 = new Pagination(4,3);

It creates a Pagination control with 4 pages and the current selected index equal to 3.

Methods used in pagination control:

Callback<P,R>

Where P and R

P – The type of the argument provided to the call method.

R – The type of the return type of the call method.

It is used when there is require a callback.

PageFactory Property

The pageFactory callback function that is called when a page has been selected.

setPageFactory(new Callback<Integer, Node>()

Sets the page factory callback function.

Example of pagination:


package javafxtuts;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;

public class Javafxtuts extends Application {

  public static void main(String[] args) {
     Application.launch(args);
  }

  
  //function which create a page in HBox and return HBox
 public HBox createPage(int pageIndex) {
        HBox hBox = new HBox();
        //To set alignment to centre
        hBox.setAlignment(Pos.CENTER);
        //set Padding is used for adding padding to HBox from other nodes
        hBox.setPadding(new Insets(0, 0, 50, 0));
        //PageIndex start from 0 so that we add 1
        Label pageLabel = new Label("The page no is " + (pageIndex+1));
        hBox.getChildren().add(pageLabel);
        return hBox;
    }
  @Override
  public void start(Stage stage) {
      stage.setTitle("Javafxtuts.com");
      //To creates a Pagination control with 4 pages and the current selected index equal to 3
      Pagination pagination = new Pagination(4, 3);
      //to create page and set it to the pagination
       pagination.setPageFactory(new Callback() {
            @Override
            public Node call(Integer pageIndex) {
                return createPage(pageIndex);
            }
        });
      BorderPane root = new BorderPane();
      root.setBottom(pagination);
        Scene scene = new Scene(root, 300, 200);
        stage.setScene(scene);
        stage.show();
  }
}

Output :

Explanation of date picker example:

  • To creates a Pagination control with 4 pages and the current selected index equal to 3Pagination pagination = new Pagination(4, 3);
  • setPageFactory(Callback)Is used to set the pages to the Pagination using Callback interface.
  • TO setting pages first we create pageSo for this purpose we build a createPage function which takes Pageindex as an argument and return HBox.

    HBox contains a Label, the text of label is “The page no is ” and “pageIndex+1”, AS

    
    public HBox createPage(int pageIndex) {
            HBox hBox = new HBox();
            //To set alignment to centre
            hBox.setAlignment(Pos.CENTER);
            //set Padding is used for adding padding to HBox from other nodes
            hBox.setPadding(new Insets(0, 0, 50, 0));
            //PageIndex start from 0 so that we add 1
            Label pageLabel = new Label("The page no is " + (pageIndex+1));
            hBox.getChildren().add(pageLabel);
            return hBox;
    }
    
  • And finally add pagination to the BorderPane layout in Bottom side.
Share.

Leave A Reply