Lakshitha Kanchana
Lakshitha Kanchana

Reputation: 1235

How to make JavaFX ScrollPane autoscroll slowly to the bottom?

I have a Scrollpane with large-enough content to active the vertical scroll bar. fx:id of the Scrollpane is myScrollPane. I also have a button called Scroll To The Bottom. I have set the Action Event of the Scroll To The Bottom button as below in the fxml controller.

@FXML
private voide myButtonOnAction(ActionEvent evt) {
    myScrollPane.setVvalue(1.0);
}

This,however, scrolls to the bottom very fast.It can't be told whether it was scrolled either. I want to know a way to make Scrollpane scrolls SLOWLY.

Upvotes: 1

Views: 2161

Answers (3)

Lakshitha Kanchana
Lakshitha Kanchana

Reputation: 1235

@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);
}

Upvotes: -1

VGR
VGR

Reputation: 44293

Do not use a Timer for this, unless you are prepared to wrap each update to the scrollbar value in a call to Platform.runLater.

The correct approach is to use a Timeline animation:

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

Upvotes: 10

Himanshu Chaudhary
Himanshu Chaudhary

Reputation: 470

To be quick I will use a Timer and gradually increment the vValue to 1. You can refer to this thread to see how to use Timer in Java

Upvotes: 2

Related Questions