JavaFX – Pagination


JavaFX – Pagination



”;


The Pagination control is a UI component that allows the user to navigate through different pages of content. For example, we can use this feature to display search results, articles, images or any other type of content that requires multiple pages. A typical pagination control has a page navigation area, previous page button and next page button as shown in the figure below −


Sample Pagination

Creating a Pagination in JavaFX

In JavaFX, the pagination control is created by instantiating a class named Pagination. This class belongs to the package javafx.scene.control. Constructors of this class are listed below −

  • Pagination() − It will generate a pagination control without page count and the default page index will be zero. However, we can also use this default constructor and set these properties later using the setPageCount() and setCurrentPageIndex() methods.
  • Pagination(int pageCount) − It will create a pagination control with the specified number of pages.
  • Pagination(int pageCount, int currentIndex) − It constructs a new Pagination control with the given number of pages and page index.

While creating a pagination in JavaFX, our first step would be instantiating the Pagination class by using any of the above mentioned constructors. Next, define a layout pane, such as Vbox or Hbox by passing the Pagination object to its constructor. Lastly, set the scene and stage to display the pagination control on the screen.

Example

The following JavaFX program demonstrates how to create a Pagination within JavaFX application. Save this code in a file with the name PaginationJavafx.java.


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.util.Callback;
import javafx.geometry.Pos; 
public class PaginationJavafx extends Application {
   private Pagination paging;
   @Override
   public void start(Stage stage) {
      // instantiating Pagination class
      paging = new Pagination(5);
      // to change the page number 
      paging.setPageFactory(new Callback <Integer, Node>() {
         @Override
         public Node call(Integer pageIndex) {
            return new Label("Page No." + (pageIndex + 1));
         }
      });
      // creating Vbox that will contain the pagination
	  VBox box = new VBox();
      box.getChildren().addAll(paging);
      box.setAlignment(Pos.CENTER);
      // creating scene and stage
      Scene scene = new Scene(box, 400, 300);
      stage.setScene(scene);
      stage.setTitle("Pagination in JavaFX");
      stage.show();
   }
   // to launch the application
   public static void main(String[] args) {
      launch(args);
   }
}

To compile and execute the saved Java file from the command prompt, use the following commands −


javac --module-path %PATH_TO_FX% --add-modules javafx.controls PaginationJavafx.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls PaginationJavafx

Output

When we execute the above code, it will generate the following output.


Pagination Output

Defining Current Index Page

One of the constructor of Pagination class accepts two parameters namely page count and current index. Whenever we use this contructor, it will generate a pagination control with the specified number of pages and the currently selected index number.

Example

In the following example, we are creating a pagination control with currently selected index number as 3. Save this code in a file with the name JavafxPagination.java.


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.util.Callback;
import javafx.geometry.Pos; 
public class JavafxPagination extends Application {
   private Pagination paging;
   @Override
   public void start(Stage stage) {
      // creating pagination with 10 pages and currently selected index as 3
      paging = new Pagination(10, 2);
      // to change the page number 
      paging.setPageFactory(new Callback <Integer, Node>() {
         @Override
         public Node call(Integer pageIndex) {
            return new Label("Page No." + (pageIndex + 1));
         }
      });
      // creating Vbox that will contain the pagination
	  VBox box = new VBox();
      box.getChildren().addAll(paging);
      box.setAlignment(Pos.CENTER);
      // creating scene and stage
      Scene scene = new Scene(box, 400, 300);
      stage.setScene(scene);
      stage.setTitle("Pagination in JavaFX");
      stage.show();
   }
   // to launch the application
   public static void main(String[] args) {
      launch(args);
   }
}

Compile and execute the saved Java file from the command prompt by using the following commands −


javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxPagination.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxPagination

Output

On executing the above code, it will generate the following output.


Pagination Output2

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *