JavaFX Tutorial - JavaFX ScrollBar








The ScrollBar class often comes with a scrollable panes.

// w  w  w  .  j  a v  a  2s .c o m
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage stage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 200);
        stage.setScene(scene);

        ScrollBar s1 = new ScrollBar();
       
        root.getChildren().add(s1);
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

A scroll bar has four areas:

  • thumb
  • right button or down button
  • left buttons or up buttons
  • track

The code above generates the following result.

null




Creating a Scroll Bar

The following code creates a scroll bar by using its default constructor.

ScrollBar sc = new ScrollBar();

The setMin and setMax methods define the minimum and maximum values represented by the scroll bar.

setValue method sets the current value of the scroll which also set the position of the thumb.

sc.setMin(0);
sc.setMax(100);
sc.setValue(50);

When a user moves the thumb, the value of the scroll bar changes.

By default, the scroll bar is oriented horizontally. We can set the vertical orientation by using the setOrientation method.

We can click the left and right button for horizontal scroll bar or down and up button for the vertical scroll bar to scroll by a unit increment. The UNIT_INCREMENT property sets this value.

Clicking on the track enables the scroll bar to move by a block increment. The BLOCK_INCREMENT property defines this value.

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.stage.Stage;
/*  w  ww. j  a v  a 2  s. c om*/
public class Main extends Application {
    @Override
    public void start(Stage stage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 200);
        stage.setScene(scene);

        ScrollBar s1 = new ScrollBar();
        s1.setMax(500);
        s1.setMin(0);
        s1.setValue(100);
        s1.setUnitIncrement(30);
        s1.setBlockIncrement(35);
        
        s1.setOrientation(Orientation.VERTICAL);
        
        root.getChildren().add(s1);
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

The code above generates the following result.

null




Scroll Event

The following code adds event handlers for the scroll event from the scroll bar.

/*from ww  w. j  a  v  a 2s.  c o  m*/
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage stage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 200);
        stage.setScene(scene);

        ScrollBar s1 = new ScrollBar();

        s1.valueProperty().addListener((ObservableValue<? extends Number> ov, 
            Number old_val, Number new_val) -> {
                System.out.println(-new_val.doubleValue());
        });          
        root.getChildren().add(s1);
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

The code above generates the following result.

null