Search code examples
javacssjavafxscrollpane

How to limit the amount of scrolling ScrollPane does


When scrolling on my ScrollPane, it leaves a lot of empty space when I reach the bottom, I want this to be limited so it stops allowing scrolling when the bottom item of the ScrollPane becomes visible.

Image 1 - Top of the ScrollPane Image 1, top of the ScrollPane

Image 2 - Midway down the ScrollPane Image 2, top of the ScrollPane

As you can see there's a lot of space at the bottom, ideally this would stop being able to scroll after "TestIdleLevel2" becomes visible.

ScrollPane:

ScrollPane itemCollectionScrollPane = new ScrollPane(itemCollection);
    itemCollectionScrollPane.setPrefSize(shopInterface.getPrefWidth()*1.55, shopInterface.getPrefHeight());
    itemCollectionScrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
    itemCollectionScrollPane.getStylesheets().add(Shop.class.getResource("/cssFolder/TransparentScrollPane.css").toExternalForm());

Transforms for the items within it:

itemBox.getTransforms().add(new Translate(shopInterface.getTranslateX() - itemBox.getPrefWidth() / 8,
                shopInterface.getTranslateY() + (counter * shopInterface.getPrefHeight() / 3), 0));
        itemCollection.getChildren().addAll(itemBox);

Each transform is separated by a counter to space it out, this stops after the items have been looped through (4). The preferred height is set to about 300, and should be extended to about 400 due to the ScrollPane. However, the ScrollPane is extending it to around 500-600.


Solution

  • Issue: itemCollection was being initialized as the same size as shopInterface.

    Solution: Initialize the height as a smaller instance of shopInterface. (In this case the exact amount was shopInterface.getHeight()/4).

    Special thanks to Fabian.