How to get thumb value range of ScrollBar in JavaFX?

Viewed 800

For instance say we have a scrollbar for showing/manipulating data on some application. Scrollbar has min, max values and visible amount as well. But there is a bit confusing thing about the range of the values to be shown according scrollbar value.

As an example here is an image

and

But it is calculated according different sides of the thumb of the scrollbar.

Say I need to show some data from database, and min value will be min id and max value will be max id as well. and consider I want to show 100 items, and I am going to set visible amount to 100 in scrollbar. While scrolling If the value of scroolbar is equal to 1, and getting the range from the database will be as simple as just adding 100 (visible amount) to 1, but this won't work when the value is equal to max. So that is why I need to get value range of the scrollbar thumb, but when looking at scrollbar source code I could not find the implementation of this feature.

How to get min and max values of the thumb or visible value range of the scrollbar?

2 Answers

The ScrollBar indicates a position, not a range. The position is available via the value property. The visualAmount property determines the size of the slider.

If value == max the thumb is in the rightmost/bottommost position. Your "max value of thumb" would exceed max.

You should therefore decide how many items to display and calculate visibleAmount and max based on this:

max           = itemCount - displayedItems
visibleAmount = max * displayedItems / itemCount

Example for a ScrollPane-like implementation:

@Override
public void start(Stage stage) {
    ScrollBar scrollBar = new ScrollBar();
    scrollBar.setOrientation(Orientation.VERTICAL);
    StackPane.setAlignment(scrollBar, Pos.CENTER_RIGHT);

    VBox container = new VBox();
    StackPane.setAlignment(container, Pos.TOP_LEFT);

    StackPane root = new StackPane(container, scrollBar);

    InvalidationListener listener = o -> {
        // adjust scrollbar properties on resize of root or content
        double rootHeight = root.getHeight();
        double contentHeight = container.getHeight();

        double max = Math.max(0, contentHeight - rootHeight);
        scrollBar.setMax(max);
        scrollBar.setVisibleAmount(max * rootHeight / contentHeight);
    };
    root.heightProperty().addListener(listener);
    container.heightProperty().addListener(listener);

    // move container up based on the scrollbar value
    container.translateYProperty().bind(scrollBar.valueProperty().negate());

    // generate some content
    for (int i = 0; i < 10; i++) {
        Rectangle rect = new Rectangle(100, 100, (i & 1) == 0 ? Color.BLACK : Color.LIGHTGRAY);
        container.getChildren().add(rect);
    }

    Scene scene = new Scene(root, 300, 300);
    stage.setScene(scene);
    stage.show();
}

The answer is very simple. Let the height of scrollbar be 600 and height of thumb is 200. Now if you substract the thumb's height and calculate the value to scroll according to leftover height(400) then there is no need of repressing the max value from the end.

You need to use some condition so that the thumb may not exceed the size. As here the thumb(200px) should not exceed 400px height as then it will not be visible to user.

Related