Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

How to use the JavaFX Pagination | 100% Perfect Tutorial

October 24, 2022 - Updated on October 29, 2022
in Java
Reading Time: 5 mins read
0
JavaFX Pagination
293
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

Toggle
  • How to use the JavaFX Pagination
  • JavaFX Pagination Examples
    • How to create the JavaFX pagination
    • How to add the node to the Scene Graph
      • Output
    • Change the page indicator style
      • Output
    • Full code example 1: How to add content to the JavaFX Pagination
      • Output
    • Full code example 2: How to add content to the JavaFX Pagination
      • Output
    • Adding CSS to the JavaFX Pagination
  • YouTube Video

How to use the JavaFX Pagination

JavaFX Pagination is very useful when your application displays a large single content. Creating the Pagination in JavaFX is very easy and there are two ways to create this node or control it in your JavaFX application. You can either use the Scene Builder or code it manually to create this node.

This pagination control is very user-friendly, when you put in place this node in your application it will show the page count, and you can also click the buttons to go to the other page. The pagination control has two areas and these are the Content Area and the Navigation Area.

The Content Area displays the content of the page, for example, images or text. The Navigation Area has buttons for the user that allow it to go to another page when it is clicked.

In this tutorial, you will learn how to use the JavaFX Pagination and put it in place in your application. I will walk you through creating this control using the Scene Builder and code it manually and add some content to the pages. Go ahead to the examples below to learn more about the JavaFX Pagination.

JavaFX Pagination Examples

The first example we will learn is by creating this node or control. The examples provided below are very easy to follow and learn. Using the WYSIWYG is very easy to do; you simply drag and drop the node to your scene graph, and this is the power of using the Scene Builder. You can watch the video tutorial below to learn more about it.

How to create the JavaFX pagination

To create the Pagination in JavaFX when we code is very easy, we will create an instance of the Pagination Class. While instantiating the constructor, we can enter some parameters into it or either leave it as nothing. The parameter uses an integer for the parameter to work because it is used for page count. The example code snippet below will show you how to create a pagination control in JavaFX.

// create the Pagination (Indeterminate)
Pagination pagination = new Pagination();

// create the Pagination with page count
Pagination pagination = new Pagination(10);

How to add the node to the Scene Graph

It is important for the node to display in your application, by adding the node to the scene, we will create the layout in JavaFX and also create the Scene. If you are using the Scene Builder to create the GUI, you can watch the video to learn how to load or display the FXML document. The example code below will show you how to add the node to the scene.

// create the JavaFX Layout
StackPane layout = new StackPane();
// create the Pagination
Pagination pagination = new Pagination(10);
// add the pagination to the layout
layout.getChildren().add(pagination);
// create the Scene
Scene scene = new Scene(layout, 500, 500); // 500x500 is the scene size
// add to the stage
stage.setScene(scene);
stage.setTitle("Understanding the Pagination");
stage.show();

Output

JavaFX Pagination Scene

Change the page indicator style

The default page indicator for the pagination is the numeric button and the other one is the bullet button. If you want to change the page indicator to the other one, you can use the getStyleClass() method. The example code below will show you how to do it.

Pagination pagination = new Pagination(10);
pagination.getStyleClass().add(Pagination.STYLE_CLASS_BULLET);

Output

JavaFX Pagination Bullet

Full code example 1: How to add content to the JavaFX Pagination

When adding content to the pagination control, we need to use the setPageFactory() method. This method is very important to use the JavaFX Pagination, without this method, your pagination is useless. It is used to generate pages and whatever node you want to display with the setPageFactory() method, it calls the call() method to return it to the Content Area. The example code below will show you how to add content to the pagination in JavaFX.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        Pagination pagination = new Pagination(10);
        pagination.setPageFactory(this::getPage);
        pagination.setMaxPageIndicatorCount(5);
        StackPane layout = new StackPane(pagination);
        Scene scene = new Scene(layout, 500, 500);
        stage.setTitle("Understanding the Pagination");
        stage.setScene(scene);
        stage.show();
    }

    private Label getPage(int pageIndex){
        Label content = new Label("This is the content for page "+(pageIndex + 1));
        return content;
    }

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

Output

How to use the JavaFX Pagination

Full code example 2: How to add content to the JavaFX Pagination

This example is different compared to the first full code example. The content of it consists of images that will display in the pagination content area. I will only show the controller class here and go to this link if you want to see the whole project.

import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Pagination;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class PaginationController implements Initializable {

    @FXML
    private Pagination pagination;
    private File files[];

    @FXML
    void getImages(ActionEvent event) {
        Stage stage = (Stage) pagination.getScene().getWindow();
        openImages(stage);
        pagination.setPageFactory(this::getPages);
    }

    public VBox getPages(int pageIndex){
        ImageView imageView = new ImageView();
        File pngs = files[pageIndex];
        try{
            BufferedImage bi = ImageIO.read(pngs);
            Image image = SwingFXUtils.toFXImage(bi,null);
            imageView.setFitWidth(500);
            imageView.setFitHeight(500);
            imageView.setImage(image);
            imageView.setSmooth(true);
        }catch (IOException e){

        }
        VBox layout = new VBox(imageView);
        return layout;

    }

    private void openImages(Stage parentStage){
        DirectoryChooser chooser = new DirectoryChooser();
        chooser.setTitle("Select a folder");
        chooser.setInitialDirectory(new File("C:\\"));
        File getFiles = chooser.showDialog(parentStage);
        if(getFiles != null){
            FilenameFilter filter = new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    return name.toLowerCase().endsWith(".png");
                }
            };
            files = getFiles.listFiles(filter);
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        pagination.setPageCount(12);

    }
}

Output

How to use the Pagination in JavaFX

Adding CSS to the JavaFX Pagination

You can also add CSS to your JavaFX application. JavaFX CSS is almost like CSS for the web. Adding CSS to the JavaFX Pagination is very easy. Go ahead below to learn more about Pagination CSS.

.pagination {
 -fx-page-information-visible: false;
}
.pagination > .page {
 -fx-background-color: lightgray;
}
.pagination > .pagination-control > .control-box {
 -fx-padding: 2;
 -fx-border-style: dashed;
 -fx-border-width: 1;
 -fx-border-radius: 5;
 -fx-border-color: blue;
}

YouTube Video

YouTube video
Previous Post

How to use the Accordion in JavaFX | 100% Perfect Tutorial

Next Post

How to show Tooltip in JavaFX | 100% Perfect Tutorial

KENSOFT

KENSOFT

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java

Related tutorials

How to Use the JavaFX Pie Chart 100% For Beginners
Java

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
205
How to Connect to an API Using JavaFX
Java

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
215
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
586
Next Post
How to show Tooltip in JavaFX

How to show Tooltip in JavaFX | 100% Perfect Tutorial

How to use the JavaFX SplitPane | 100% Perfect Tutorial

How to use the JavaFX SplitPane | 100% Perfect Tutorial

How to use the Slider in JavaFX | 100% Perfect Tutorial

How to use the Slider in JavaFX | 100% Perfect Tutorial

Leave a Reply Cancel reply

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

Tools

Multi-platform installer builder

Java profiler

  • Trending
  • Comments
  • Latest
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on January 23, 2023
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on July 3, 2022
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
hide and show password in jPasswordField

JPasswordField in Java Hide or Show Password | 100% best for beginners

April 2, 2021 - Updated on September 21, 2022
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

set up JavaFX and Scene Builder

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
How to take a screenshot on PC using Kenshot

How to Take a Screenshot on PC Using Kenshot: A Full Guide

January 18, 2024 - Updated on October 6, 2024

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

Popular Tutorials

  • MySQL database using XAMPP

    How to connect Java to MySQL database using Xampp server | 100% best for beginners

    0 shares
    Share 0 Tweet 0
  • Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    0 shares
    Share 0 Tweet 0
  • How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

    0 shares
    Share 0 Tweet 0
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

What’s up! I'm Kent. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java, which I use to create computer applications. In a company, I created applications and a website.

Categories

Website

Check the status

Privacy Policy

Terms and Condition

Sitemap

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

© 2024 Made With Love By KENSOFT PH

No Result
View All Result
  • Download
    • Kenshot
  • Contact
  • About
  • Java Quiz

© 2024 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.