JavaFX auto-scroll down scrollpane

后端 未结 6 943
轻奢々
轻奢々 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:17

    I did it by using an AnimationTimer. I had to wait for 100000000 nanoseconds to be sure that the ScrollPane had reacted to the expanded Titlepane.

    public void scrollNodeInTopScrollPane(Node n, ScrollPane s) {
        final Node node = n;
        final ScrollPane clientTopScrollPane = s;
        AnimationTimer timer = new AnimationTimer() {
            long lng = 0;
            @Override
            public void handle(long l) {
                if (lng == 0) {
                    lng = l;                   
                }                
                if (l > lng + 100000000) {                   
                    if (node.getLocalToSceneTransform().getTy() > 20) {                      
                        clientTopScrollPane.setVvalue(clientTopScrollPane.getVvalue() + 0.05);                        
                        if (clientTopScrollPane.getVvalue() == 1) {
                            this.stop();                          
                        }
                    } else {
                        this.stop();
                    }
                }                
            }
        };
        timer.start();
    }
    

提交回复
热议问题