4

I have a scrollpane on my screen and a vbox inside it. I add numerous checkboxes in this vbox (and I see it expanding) but the scrollpane doesn't seem to know that it should start showing a scrollbar when the content exceeds the height of that pane. I already tried changing the scrollbar policy but it just shows a scrollbar, I can't actually scroll. How do I fix this?

private void addCheckbox(String checkbox){
        CheckBox c = new CheckBox(checkbox);
        c.setPadding(this.paddingCheckBoxes);
        c.setSelected(true);
        this.vBoxFilters.getChildren().add(c);
    }

Here's the fxml:

<StackPane>
     <children>
          <ScrollPane fx:id="scrollPaneFilters" prefHeight="878.0" prefWidth="260.0">
            <content>
              <VBox fx:id="vBoxFilters" minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" />
            </content>
          </ScrollPane>
     </children>
  </StackPane>

enter image description here

Hasen
  • 11,710
  • 23
  • 77
  • 135
Student2020
  • 65
  • 1
  • 1
  • 9

3 Answers3

16

Try to remove prefHeight for VBox in your fxml

jns
  • 6,017
  • 2
  • 23
  • 28
5

For others who are searching for the same question, the same problem can occur if you put the VBox inside an AnchorPane inside the ScrollPane. This will happen in SceneBuilder if you use "Scroll Pane" instead of "Scroll Pane (empty)".

geometrikal
  • 3,195
  • 2
  • 29
  • 40
-1

First of all make sure you didn’t set a defined height on your main container the “VBox” then for your scroll pane to work you have to set a height. Example:

ScrollPane scrollPane = new ScrollPane(); scrollPane.setContent(scrollComposition); scrollPane.setMaxHeight(740);

Viand Direct
  • 489
  • 1
  • 4
  • 4
  • no need for the max height on the scrollpane - its parent layout should be choosen to take care of the .. layout ;) As a general rule: do not set layout constraints, ever (there are exceptions, of course, this most certainly isn't). BTW: please format any code as .. code ;) – kleopatra Oct 15 '21 at 09:44