如何使JavaFX ScrollPane自动滚动到底部?

时间:2017-01-18 15:56:18

标签: java javafx scrollpane

我有一个Scrollpane,内容足够大,可以激活垂直滚动条。 fx:id的{​​{1}}为Scrollpane。 我还有myScrollPane名为button。 我在Scroll To The Bottom控制器中设置了Action Event Scroll To The Bottom的{​​{1}},如下所示。

button
然而,这很快就滚动到底部。不能告诉它是否也滚动了。我想知道一种让fxml慢慢滚动的方法。

3 个答案:

答案 0 :(得分:7)

请勿使用计时器,除非您准备在调用Platform.runLater时将每个更新包装到滚动条值。

正确的方法是使用Timeline动画:

static void slowScrollToBottom(ScrollPane scrollPane) {
    Animation animation = new Timeline(
        new KeyFrame(Duration.seconds(2),
            new KeyValue(scrollPane.vvalueProperty(), 1)));
    animation.play();
}

答案 1 :(得分:2)

为了快速,我将使用一个Timer并逐渐将vValue递增为1.您可以参考this线程来了解如何在Java中使用Timer

答案 2 :(得分:-1)

@FXML
private void myButtonOnAction(ActionEvent evt) {
    double vVal = myScrollPane.getVvalue();
    double d = (1.0-vVal)/100;
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        boolean isTheStart = true;
        double difference;
        @Override
        public void run() {
            double currentVVal = myScrollPane.getVvalue();
            if (isTheStart) {
                isTheStart = false;
                difference = (1.0 - currentVVal)/100;
            }
            myScrollPane.setVvalue(currentVVal+difference);
            if (currentVVal >= 1.0) {
                this.cancel();
            }
        }
    }, 1*500, 10);
}