In my application I am appending the text to TextArea
every 2 minutes. When ever I append new line to TextArea
the auto scroll automatically going to down. But I want to stay the scroll where i am keep the scroll button. How to do it in JavaFX.
logTextArea.appendText("Here i am appending text to text area"+"\n");
logTextArea.setScrollTop(Double.MIN_VALUE);
I tried this but scroll automatically going to down , but I need to keep my scroll chosen position I don't want to go automatically down.
How can I do this?
The most straightforward way is to remember the position of the caret and restore it after it gets moved by
appendText
orsetText
.Here's how you can do it:
Maybe you could just add a changeListener to the TextArea which does nothing, or it just scrolls to the top of the TextArea everytime the text inside it is changed.
Now when you
logTextArea.appendText("Here i am appending text to text area"+"\n");
to the TextArea, it should stay at the top.The idea is taken from: JavaFX TextArea and autoscroll
You can write your own function to append text. In this method you can use
setText
, rather thanappendText
asappendText
automatically scrolls to the end of the content (setText
scrolls to the beginning but this can be supressed by setting back thescrollTopProperty
to its previous value).Example
Note:
Alternatively you can also extend
TextArea
and overloadappendText
to be able to specify whether you want to move the scrollbar:and the usage: