JavaFX: Working with JavaFX UI Components

10 Scroll Bar

This chapter explains how to create scrollable panes by using the scroll bar control.

The ScrollBar class enables you to create scrollable panes and views in your application. Figure 10-1 shows the three areas of a scroll bar: the thumb, the right and left buttons (or down and up buttons), and the track.

Figure 10-1 Elements of the scroll bar

The track, the thumb, and the left/right buttons
Description of "Figure 10-1 Elements of the scroll bar"

Creating a Scroll Bar

Take a moment to review the code fragment in Example 10-1.

Example 10-1 Simple Scroll Bar

ScrollBar sc = new ScrollBar();

The setMin and setMax methods define the minimum and maximum values represented by the scroll bar. When a user moves the thumb, the value of the scroll bar changes. In Example 10-1, the value equals 50, so when the application starts, the thumb is in the center of the scroll bar. By default, the scroll bar is oriented horizontally. However, you can set the vertical orientation by using the setOrientation method.

The user can click the left or right button (down or up button for the vertical orientation) to scroll by a unit increment. The UNIT_INCREMENT property specifies the amount by which the scroll bar is adjusted when a button is clicked. Another option is clicking within the track by a block increment. The BLOCK_INCREMENT property defines the amount by which the scroll bar is adjusted when the track of the bar is clicked.

In your application, you can use one of several scroll bars to scroll through graphical content that exceeds the borders of the available space.

Using a Scroll Bar in Your Application

Examine the scroll bar in action. The application shown in Example 10-2 implements a scrollable scene to view the images. The task of this application is to enable users to view the content of the vertical box, which is longer than the scene's height.

Example 10-2 Scrolling Through Multiple Images

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Orientation;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ScrollBarSample extends Application {
    final ScrollBar sc = new ScrollBar();
    final Image[] images = new Image[5];
    final ImageView[] pics = new ImageView[5];
    final VBox vb = new VBox();
    DropShadow shadow = new DropShadow();
    public void start(Stage stage) {
        Group root = new Group();
        Scene scene = new Scene(root, 180, 180);
        root.getChildren().addAll(vb, sc);
        for (int i = 0; i < 5; i++) {
            final Image image = images[i] =
                new Image(getClass().getResourceAsStream(
                    "fw" +(i+1)+ ".jpg")
            final ImageView pic = pics[i] =
                new ImageView(images[i]);
        sc.valueProperty().addListener((ObservableValue<? extends Number> ov, 
            Number old_val, Number new_val) -> {
    public static void main(String[] args) {

The first lines of the code add a vertical box with the images and a scroll bar to the scene.

The Y coordinate of the vertical box changes when the VALUE property of the scroll bar is changed, so that each time the thumb is moved, or either a button or the track is clicked, the vertical box moves, as shown in Example 10-3.

Example 10-3 Implementing the Scrolling of the Vertical Box

sc.valueProperty().addListener((ObservableValue<? extends Number> ov, 
    Number old_val, Number new_val) -> {

Compiling and running this application produces the output shown in Figure 10-2.

Figure 10-2 Scroll Bar Sample

A scrollbar sample
Description of "Figure 10-2 Scroll Bar Sample "

This application shows one typical use of the ScrollBar class. You can also customize this class to create a scroll area within a scene. As for every UI control and every node, the scroll bar can be styled to change its appearance from the default implementation.

Related API Documentation  

Close Window

Table of Contents

JavaFX: Working with JavaFX UI Components

Expand | Collapse