JavaFX: after setting text in textArea setting scroll down in a separate thread does not work

I created one JavaFX application where I update the log with one background process. So I set the log text to TextArea and set the scroll down using logs.setScrollTop(Double.MAX_VALUE) . but the scroll bar is set a little bit from bottom to top. I also tried TextFlow inside ScrollPan and set the scroll down using logDisplay.setVvalue(1.0) . It also gives the same result.

 Platform.runLater(() -> { logs.setText([setting log text]);//TextArea logs logs.setScrollTop(Double.MAX_VALUE)); }); //For TextFlow inside ScrollPane Platform.runLater(() -> { logs.setText([setting log text]);//Text logs logDisplay.setVvalue(1.0); }); 

I also tried to run the code in a separate thread, for example

 new Thread() { public void run(){ System.out.println("called set test"); logs.setText([setting log text]);//Text logs logDisplay.setVvalue(1.0); } }.start(); 

But nothing works :(
Can you help me with this wrong?
Thanks

- Edit--
The problem seems to be related to the problem of streaming. The scrollbar value is updated to the previous text value. In fact, when retrieving the scroll value, it does not get the last value, but gets the old value, so the scroll bar is set to the end of the previous message, and not the last last line.

+6
source share
1 answer

I do not know the real problem of this problem, but found an alternative solution.

I set the caret position at the end of the text using the length of the text.

 logs.setText(logText); logs.positionCaret(logText.length()); 

It works for me. :)

+5
source

Source: https://habr.com/ru/post/986055/


All Articles