devstory

Le Tutoriel de JavaFX ScrollPane

  1. JavaFx ScrollPane
  2. Exemple de ScrollPane
  3. Exemple de  ScrollPane et Pannable

1. JavaFx ScrollPane

ScrollPane est un composant d'interface défilant (scrollable), il sert à afficher un grand contenu dans un espace limité. Il contient des barres de traction horizontale et verticale.
ScrollBar Policy
Vous pouvez configurer les options d’affichage dans la barre de défilement (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. Exemple de ScrollPane

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);
    }
}
Exécutez l'exemple :

3. Exemple de  ScrollPane et Pannable

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);
    }
}
Exécutez l'exemple :

Tutoriels de JavaFX

Show More