- JavaFX Tutorial
- JavaFX - Home
- JavaFX - Overview
- JavaFX Installation and Architecture
- JavaFX - Environment
- JavaFX - Installation Using Netbeans
- JavaFX - Installation Using Eclipse
- JavaFX - Installation using Visual Studio Code
- JavaFX - Architecture
- JavaFX - Application
- JavaFX 2D Shapes
- JavaFX - 2D Shapes
- JavaFX - Drawing a Line
- JavaFX - Drawing a Rectangle
- JavaFX - Drawing a Rounded Rectangle
- JavaFX - Drawing a Circle
- JavaFX - Drawing an Ellipse
- JavaFX - Drawing a Polygon
- JavaFX - Drawing a Polyline
- JavaFX - Drawing a Cubic Curve
- JavaFX - Drawing a Quad Curve
- JavaFX - Drawing an Arc
- JavaFX - Drawing an SVGPath
- JavaFX Properties of 2D Objects
- JavaFX - Stroke Type Property
- JavaFX - Stroke Width Property
- JavaFX - Stroke Fill Property
- JavaFX - Stroke Property
- JavaFX - Stroke Line Join Property
- JavaFX - Stroke Miter Limit Property
- JavaFX - Stroke Line Cap Property
- JavaFX - Smooth Property
- Operations on 2D Objects
- JavaFX - 2D Shapes Operations
- JavaFX - Union Operation
- JavaFX - Intersection Operation
- JavaFX - Subtraction Operation
- JavaFX Path Objects
- JavaFX - Path Objects
- JavaFX - LineTo Path Object
- JavaFX - HLineTo Path Object
- JavaFX - VLineTo Path Object
- JavaFX - QuadCurveTo Path Object
- JavaFX - CubicCurveTo Path Object
- JavaFX - ArcTo Path Object
- JavaFX Color and Texture
- JavaFX - Colors
- JavaFX - Linear Gradient Pattern
- JavaFX - Radial Gradient Pattern
- JavaFX Text
- JavaFX - Text
- JavaFX Effects
- JavaFX - Effects
- JavaFX - Color Adjust Effect
- JavaFX - Color input Effect
- JavaFX - Image Input Effect
- JavaFX - Blend Effect
- JavaFX - Bloom Effect
- JavaFX - Glow Effect
- JavaFX - Box Blur Effect
- JavaFX - GaussianBlur Effect
- JavaFX - MotionBlur Effect
- JavaFX - Reflection Effect
- JavaFX - SepiaTone Effect
- JavaFX - Shadow Effect
- JavaFX - DropShadow Effect
- JavaFX - InnerShadow Effect
- JavaFX - Lighting Effect
- JavaFX - Light.Distant Effect
- JavaFX - Light.Spot Effect
- JavaFX - Point.Spot Effect
- JavaFX - DisplacementMap
- JavaFX - PerspectiveTransform
- JavaFX Transformations
- JavaFX - Transformations
- JavaFX - Rotation Transformation
- JavaFX - Scaling Transformation
- JavaFX - Translation Transformation
- JavaFX - Shearing Transformation
- JavaFX Animations
- JavaFX - Animations
- JavaFX - Rotate Transition
- JavaFX - Scale Transition
- JavaFX - Translate Transition
- JavaFX - Fade Transition
- JavaFX - Fill Transition
- JavaFX - Stroke Transition
- JavaFX - Sequential Transition
- JavaFX - Parallel Transition
- JavaFX - Pause Transition
- JavaFX - Path Transition
- JavaFX Images
- JavaFX - Images
- JavaFX 3D Shapes
- JavaFX - 3D Shapes
- JavaFX - Creating a Box
- JavaFX - Creating a Cylinder
- JavaFX - Creating a Sphere
- Properties of 3D Objects
- JavaFX - Cull Face Property
- JavaFX - Drawing Modes Property
- JavaFX - Material Property
- JavaFX Event Handling
- JavaFX - Event Handling
- JavaFX - Using Convenience Methods
- JavaFX - Event Filters
- JavaFX - Event Handlers
- JavaFX UI Controls
- JavaFX - UI Controls
- JavaFX - ListView
- JavaFX - Accordion
- JavaFX - ButtonBar
- JavaFX - ChoiceBox
- JavaFX - HTMLEditor
- JavaFX - MenuBar
- JavaFX - Pagination
- JavaFX - ProgressIndicator
- JavaFX - ScrollPane
- JavaFX - Separator
- JavaFX - Slider
- JavaFX - Spinner
- JavaFX - SplitPane
- JavaFX - TableView
- JavaFX - TabPane
- JavaFX - ToolBar
- JavaFX - TreeView
- JavaFX - Label
- JavaFX - CheckBox
- JavaFX - RadioButton
- JavaFX - TextField
- JavaFX - PasswordField
- JavaFX - FileChooser
- JavaFX - Hyperlink
- JavaFX - Tooltip
- JavaFX - Alert
- JavaFX - DatePicker
- JavaFX - TextArea
- JavaFX Charts
- JavaFX - Charts
- JavaFX - Creating Pie Chart
- JavaFX - Creating Line Chart
- JavaFX - Creating Area Chart
- JavaFX - Creating Bar Chart
- JavaFX - Creating Bubble Chart
- JavaFX - Creating Scatter Chart
- JavaFX - Creating Stacked Area Chart
- JavaFX - Creating Stacked Bar Chart
- JavaFX Layout Panes
- JavaFX - Layout Panes
- JavaFX - HBox Layout
- JavaFX - VBox Layout
- JavaFX - BorderPane Layout
- JavaFX - StackPane Layout
- JavaFX - TextFlow Layout
- JavaFX - AnchorPane Layout
- JavaFX - TilePane Layout
- JavaFX - GridPane Layout
- JavaFX - FlowPane Layout
- JavaFX CSS
- JavaFX - CSS
- Media with JavaFX
- JavaFX - Handling Media
- JavaFX - Playing Video
- JavaFX Useful Resources
- JavaFX - Quick Guide
- JavaFX - Useful Resources
- JavaFX - Discussion
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 −
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.
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.
To Continue Learning Please Login
Login with Google