I am trying to get a TextArea to autoscroll to the bottom with new text which is put in via an event handler. Each new entry is just one long string of text with each entry separated by a line break. I have tried a change handler which sets setscrolltop to Double.MIN_VALUE but to no avail. Any ideas of how this could be done?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
txa.appendText("") will scroll to the bottom without a listener. This becomes an issue if you want to scroll back and the text is being constantly updated. txa.setText("") puts the scroll bar back at the top and same issue applies.
My solution was to extend the TextArea class, ammend the FXML tag from textArea to LogTextArea. Where this works, it clearly causes problems in scene builder as it does not know what this component is
You have to add a listener to the
TextArea
element to scroll to the bottom when it's value is changed:But this listener is not triggered when you use the
setText(text)
method, so if you want to trigger it after asetText(text)
use theappendText(text)
right after it:This sounds more like a bug, once the
setText()
should trigger thechanged
listener, however it doesn't. This is the workaround I use myself and hope it helps you.Alternative to that strange setText bug without using appendText
I don't have enough reputation to comment, but wanted to give some insight for future readers as to why setText doesn't appear to trigger the listener, but appendText does, as in Math's answer.
I Just found this answer while encountering similar issues myself, and looked into the code. This is currently the top result for 'javafx textarea settext scroll' in a google search.
setText does indeed trigger the listener. According to the javadoc on the doSet method in TextInputControl (TextArea's superclass):
Inside the doSet method, a call is made to updateText(), which TextArea overrides:
So, when you set the scroll amount in the listener as in Math's answer, the following happens:
When you then append "",
The javadoc is above is clear why this is the case - doSet is only called when using setText. In fact, appendText calls insertText which calls replaceText - and the javadoc further states that replaceText does NOT trigger a call to doSet.
The behaviour is rather irritating, especially since these are all final methods, and not obvious at first glance - but is not a bug.
One addendum I would add to jamesarbrown's response would be to this would be to use a boolean property instead so you can access it from within FXML. Something like this.
However, the problem with this answer is that if you get flooded with an unreasonably large amount of input (as can happen when retrieving a log from an IO Stream) the javaFX thread will lock up because the TextArea gets too much data.