o7planning

JavaFX ScrollPane Tutorial with Examples

  1. JavaFx ScrollPane
  2. ScrollPane Example
  3. ScrollPane with Pannable example

1. JavaFx ScrollPane

ScrollPane is a scrollable component used to display a large content in a limited space. It contains horizontal and vertical sroll bars.
ScrollBar Policy
You can set up display policy for scroll bar:
  • NEVER - Never display
  • ALWAYS - Always display
  • AS_NEEDED - Display if needed.
** ScrollBarPolicy **

// Setting a horizontal scroll bar is always display
scrollPane.setHbarPolicy(ScrollBarPolicy.ALWAYS);

// Setting vertical scroll bar is never displayed.
scrollPane.setVbarPolicy(ScrollBarPolicy.NEVER);

2. ScrollPane Example

ScrollPaneDemo1.java
package org.o7planning.javafx.scrollpane;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.stage.Stage;

public class ScrollPaneDemo1 extends Application {

    @Override
    public void start(Stage primaryStage) {

        // Create a ScrollPane
        ScrollPane scrollPane = new ScrollPane();

        Button button = new Button("My Button");
        button.setPrefSize(400, 300);

        // Set content for ScrollPane
        scrollPane.setContent(button);

        // Always show vertical scroll bar
        scrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS);
       
        // Horizontal scroll bar is only displayed when needed
        scrollPane.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);

        primaryStage.setTitle("ScrollPane Demo 1 (o7planning.org)");
        Scene scene = new Scene(scrollPane, 550, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Running the example:

3. ScrollPane with Pannable example

ScrollPaneDemo2.java
package org.o7planning.javafx.scrollpane;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class ScrollPaneDemo2 extends Application {
   
    @Override
    public void start(Stage primaryStage) {
 
        final FlowPane container = new FlowPane();
       
        // Button 1
        Button button1= new Button("Button 1");
        button1.setPrefSize(350, 100);
       
        container.getChildren().add(button1);
       
        // Button 2
        Button button2= new Button("Button 2");
        button2.setPrefSize(245, 220);
       
        container.getChildren().add(button2);
     
        // ScrollPane
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(container);
       
        // Pannable.
        scrollPane.setPannable(true);
     

        primaryStage.setTitle("ScrollPane Demo 2 (o7planning.org)");
        Scene scene = new Scene(scrollPane, 550, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Running the example:

JavaFX Tutorials

Show More