0

I'm working on a javafx project that has a VBox inside a ScrollPane. The problem is that when the height of the vBox decreases, the scrollPane scrolls Up Automatically. How can i prevent that ? Knowing that it does not scrolls when the height increases!

Note: inside the vBox i have buttons which add/remove some TextFields which result in the increasing/decreasing of the VBox height.

I cannot show the whole code due to work regulations but here is where i define and add the VBox inside ScrollPane:

    VBox mainVBox = new VBox();
    mainVBox.setStyle("-fx-background-color: #0292b7");
    mainVBox.setPrefWidth(400); 
    mainVBox.setSpacing(10);
    mainVBox.setPadding(new Insets(3,5,3,5));
    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setContent(mainVBox);
    scrollPane.setPrefWidth(420);
    scrollPane.setPrefHeight(600);
    VBox.setVgrow(scrollPane,Priority.ALWAYS);
  • nobody wants to wade through your production code - you need to provide a mcve, though: stackoverflow.com/help/mcve otherwise the question is not answerable and will be closed (probably ;) – kleopatra Jan 12 '18 at 09:24
  • Do you also remove the Button which is clicked from the VBox? –  Jan 12 '18 at 09:48
  • @devpuh yes i do remove the button along with a text field and a label – Ahmed Salim Jan 12 '18 at 10:00

1 Answers1

1

The reason of this behavior is that you remove the element which currently has the focus. Which lead to a focus-transfer to the first element in the container and the ScrollPane automatically scrolls to this element.

To change this you can add for example mainVBox.requestFocus(); in you code before you remove the Button or/and TextField.