Implementing the page factory in pagination

JavaFXObject Oriented ProgrammingProgramming

Pagination divides content up between pages and allows users to skip between pages or go in order through the content. You can create pagination by instantiating the javafx.scene.control.Pagination class.

Example

The following Example demonstrates hoe to create pagination and add data to it.

import java.io.FileInputStream;
import java.io.InputStream;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Pagination;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class PaginationAction extends Application {
   public ImageView pageContent(int pageIndex){
      try{
         //Creating the image view
         ImageView imageView = new ImageView();
         //Setting the image view parameters
         imageView.setFitWidth(590);
         imageView.setFitHeight(300);
         imageView.setPreserveRatio(true);
         String [] img = {"elephant.jpg", "cat.jpg", "boy.jpg", "car.jpg", "road.jpg"};
         for(int i = pageIndex; i<pageIndex+1; i++) {
            //creating the image object
            InputStream stream = new FileInputStream("D:\\images\\"+img[i]);
            Image image = new Image(stream);
            imageView.setImage(image);
         }
         return imageView;
      }catch (Exception e) {}
      return null;
   }
   public void start(Stage stage) {
      //Creating a pagination
      Pagination pagination = new Pagination();
      //Setting number of pages
      pagination.setPageCount(5);
      //Creating contents for various pages
      pagination.setPageFactory((Integer pageIndex) -> pageContent(pageIndex));
      //Creating an anchor pane to hold the pagination
      AnchorPane pane = new AnchorPane();
      AnchorPane.setTopAnchor(pagination, 5.0);
      AnchorPane.setRightAnchor(pagination, 5.0);
      AnchorPane.setBottomAnchor(pagination, 5.0);
      AnchorPane.setLeftAnchor(pagination, 5.0);
      pane.getChildren().addAll(pagination);
      //Setting the stage
      Scene scene = new Scene(pane, 595, 330, Color.BEIGE);
      stage.setTitle("Pagination");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

raja
Published on 18-May-2020 11:37:42
Advertisements