JavaFX auto-scroll down scrollpane

后端 未结 6 946
轻奢々
轻奢々 2020-12-10 22:12

Is there any way of auto scroll down a ScrollPane control when it content\'s height increases ? For example, I have a TitledPane on the bottom of the screen (inside a Scroll

6条回答
  •  我在风中等你
    2020-12-10 22:30

    You have to tell scrollpane where is the current coordinates before pass a value to the vertical or horizontal bar.

    This code works pretty fine for me:

    // the owner's node of your scrollPane;
    titledPane.layout(); 
    // the maxValue for scrollPane bar ( 1.0 it's the default value )
    scrollPane.setVvalue( 1.0d ); 
    

    You have to code this where your scrollpane grows. For example, if you add nodes to the scrollpane inner node, them, add a listener to it's children list.

    If the inner node of scrollpane change it's height, add a listener to the heightProperty.

    For example, the inner node of your scrollPane it's an AnchorPane and you add nodes to this pane, so do like this:

    anchorPane.getChildren().addListener( 
        ( ListChangeListener.Change c ) -> {
            titledPane.layout();
            scrollPane.setVvalue( 1.0d ); 
        }
    );
    

    If it's the height which grows...

    heightProperty().addListener(
        (observable, oldValue, newValue) -> {
            titledPane.layout();
            scrollPane.setVvalue( 1.0d ); 
        }
    );
    

    That's it!

提交回复
热议问题