Run this test app :
"
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class testTextArea extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
StringBuilder string = new StringBuilder();
for (int i = 0; i < 5000; ++i) {
string.append("This is a very long text in order to test something.").append(i).append("\n");
}
HBox box = new HBox();
Scene scene = new Scene(box);
primaryStage.setScene(scene);
primaryStage.show();
TextArea textArea = new TextArea(string.toString());
box.getChildren().add(textArea);
textArea.scrollTopProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
System.out.println(newValue);
}
});
textArea.setScrollTop(1000);
}
}
"
You will see that the call to setScrollTop is not working because the method "scrollBoundsToVisible" in TextAreaSkin is being called afterwards setting a new scrollTop value to 0.
"
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class testTextArea extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
StringBuilder string = new StringBuilder();
for (int i = 0; i < 5000; ++i) {
string.append("This is a very long text in order to test something.").append(i).append("\n");
}
HBox box = new HBox();
Scene scene = new Scene(box);
primaryStage.setScene(scene);
primaryStage.show();
TextArea textArea = new TextArea(string.toString());
box.getChildren().add(textArea);
textArea.scrollTopProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
System.out.println(newValue);
}
});
textArea.setScrollTop(1000);
}
}
"
You will see that the call to setScrollTop is not working because the method "scrollBoundsToVisible" in TextAreaSkin is being called afterwards setting a new scrollTop value to 0.