This is the piece of my code.
textArea.setText(someNewText)
textArea.positionCaret(textArea.getText().length());
textArea.setEditable(true);
textArea.setScrollTop(Double.MAX_VALUE);
I use textArea.setScrollTop(Double.MAX_VALUE)
to scroll textarea to the bottom (solution I found in internet). It works, but not always. I've noted that it can not work only when vertical scroll bar is not visible before calling this code and visible after the code was executed. When vertical scroll bar is visible before calling this code then scrolling to the bottom works always. How to fix it? Maybe I should make vertical scroll bar always visible? If yes, then how - I didn't find the solution.
EDIT: This is the sample code:
public class JavaFxApp1 extends Application{
private TextArea textArea;
@Override
public void start(Stage stage) throws Exception {
Button button=new Button("Press here");
textArea=new TextArea();
VBox vbox = new VBox(button,textArea);
button.setOnAction((event)->{
textArea.appendText("###This is a very long string:some text some text some text some text some"
+ " text some text some text some text some text some text"
+ " text some text some text some text some text some text"
+ " text some text some text some text some text some text .\n");
textArea.selectEnd();
textArea.deselect();
textArea.setScrollTop(Double.MAX_VALUE);
});
textArea.setEditable(true);
textArea.setWrapText(true);
textArea.setStyle("-fx-font-size:14px;-fx-focus-color: transparent;-fx-font-family: monospace;");
Scene scene=new Scene(vbox);
stage.setTitle("SomeTitle");
stage.setScene(scene);
stage.setMinHeight(400);
stage.setMinWidth(800);
stage.show();
}
}
This is the result when I pressed button 4 times:
As you see it didn't scroll to the bottom. After I press button again (the fifth time) I have the following result:
Now, as you see it was scrolled to the bottom.
I tried to add:
ScrollPane scrollPane = (ScrollPane) textArea.lookup(".scroll-pane");
scrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS);
to make scrollbar visible always - it is visible but after 4 times anyway doesn't scroll to the bottom.
How to fix it?