ScrollPane
ScrollPane in JavaFX is a container that allows you to add content that might be larger than the visible area. It provides automatic horizontal and/or vertical scrollbars so the user can scroll through the content.
This is very useful when you have:
- long lists of items
- large images
- tables or forms that don’t fit the window
Basic Example
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ScrollPaneExample extends Application {
@Override
public void start(Stage stage) {
// Create content
VBox content = new VBox(10); // spacing = 10
for (int i = 1; i <= 30; i++) {
content.getChildren().add(new Label("Item " + i));
}
// Create ScrollPane and put the content inside
ScrollPane scrollPane = new ScrollPane(content);
// Optional: always show vertical scrollbar
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setPrefSize(200, 150); // visible area
Scene scene = new Scene(scrollPane);
stage.setTitle("ScrollPane Example");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
What happens:
- The
VBoxhas 30 labels, which is too tall for the window. - The
ScrollPaneautomatically adds a vertical scrollbar so the user can scroll through all items.
Important Properties
scrollPane.setContent(node); // set the content
scrollPane.setVbarPolicy(...); // vertical scrollbar policy
scrollPane.setHbarPolicy(...); // horizontal scrollbar policy
scrollPane.setFitToWidth(true); // make content width match ScrollPane width
scrollPane.setFitToHeight(true); // make content height match ScrollPane height
ScrollBarPolicy options:
ALWAYS→ always showNEVER→ never showAS_NEEDED→ show only if content overflows
Example with Horizontal and Vertical Scrolling
VBox vbox = new VBox(20);
for (int i = 1; i <= 50; i++) {
vbox.getChildren().add(new Label("Item " + i));
}
ScrollPane scrollPane = new ScrollPane(vbox);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);
scrollPane.setFitToWidth(true);
FXML Version
<ScrollPane xmlns:fx="http://javafx.com/fxml" prefWidth="200" prefHeight="150">
<content>
<VBox spacing="10">
<children>
<Label text="Item 1"/>
<Label text="Item 2"/>
<Label text="Item 3"/>
<!-- add more items -->
</children>
</VBox>
</content>
</ScrollPane>
Use Cases
- Scrollable forms (e.g., monitor settings page)
- Image galleries
- Long lists of files or buttons
- Any area where content might exceed visible space
Tip: Combine ScrollPane with VBox, GridPane, or FlowPane for better layouts. For example:
ScrollPane scrollPane = new ScrollPane();
FlowPane flow = new FlowPane(10, 10);
scrollPane.setContent(flow);
scrollPane.setFitToWidth(true);
This way, the FlowPane items will wrap, and the ScrollPane allows scrolling.