These are simple step by step code snippets to allow you learn JavaFX.

Example 1: Pagination

Here are the code snippets:

Step 1: Create Project

  1. Open your favorite Java IDE.
  2. In the menu go to File --> Create New Project.

Step 2: Dependencies

No dependencies are needed for this project.

Step 3: Write Code

Our code will comprise the following java files:

  • PaginationExample.java
  1. In your editor or IDE, create a file known as PaginationExample.java.
  2. Then add the following code:

(a). PaginationExample.java

After creating our class, the first thing is to define imports. Such imports are ready made classes that inject more functionalities into our project.

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;

The next step is to extend our class as shown below:

public class PaginationExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • Node call(Integer param)
  • void start(Stage primaryStage)
  • Node call(Integer param)

For our Java program to run we need a main method. Add it as shown below:

    public static void main(String[] args) {

We have 3methods to override in this class. Here are those methods:

  • public Node call(Integer param) {
  • public void start(Stage primaryStage) {
  • public Node call(Integer param) {
    Prepend the @Override modifier to your method. Then add implementation code as follows:
        @Override
        public Node call(Integer param) {
            return new Label("Content for page " + param);
        }

Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX App");

        Pagination pagination = new Pagination();
        pagination.setPageCount(21);
        pagination.setCurrentPageIndex(3);
        pagination.setMaxPageIndicatorCount(3);

        pagination.setPageFactory(new Callback<Integer, Node>() {
            @Override
            public Node call(Integer param) {
                return null;
            }

Here is the full code:

package com.jenkov.javafx.pagination;

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;

public class PaginationExample extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    public static class MyPageFactory implements Callback<Integer, Node> {
        @Override
        public Node call(Integer param) {
            return new Label("Content for page " + param);
        }
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX App");

        Pagination pagination = new Pagination();
        pagination.setPageCount(21);
        pagination.setCurrentPageIndex(3);
        pagination.setMaxPageIndicatorCount(3);

        pagination.setPageFactory(new Callback<Integer, Node>() {
            @Override
            public Node call(Integer param) {
                return null;
            }
        });

        pagination.setPageFactory((pageIndex) -> {

            Label label1 = new Label("Content for page with index: " + pageIndex);
            label1.setFont(new Font("Arial", 24));

            Label label2 = new Label("Main content of the page ...");

            return new VBox(label1, label2);
        });

        VBox vBox = new VBox(pagination);
        //HBox hBox = new HBox(button1, button2);
        Scene scene = new Scene(vBox, 960, 600);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

Download

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author
3. Code: Apache 2.0 License