0

The following question is about RichTexFX. For standard JavaFX components, I've never had this issue before.

In my application, I am adding text content to the end of a GenericStyledArea (actually a subclass of it, but that should have no impact on the behaviour I am observing). Every time new content has been added to the end of the document, I scroll to the bottom so the new content is always in view. I use the following code to scroll to bottom (actually written in Kotlin, but I have translated it to Java below):

showParagraphAtTop(document.getParagraphs().size() - 1)

This almost always works. However, in some cases the screen will not show the newly added element. I have come to the conclusion that by the time showParagraphAtTop is called, some internal state in richtextfx has not been updated. This is because I added a button that I can click on to call the code above, and if I click it after the scroll failed, it scrolls to the bottom as expected.

I have tried various ways to work around this, but the only solution that works reliably is to do the following:

FxTimer.runLater(Duration.ofMillis(10), () -> showParagraphAtTop(getDocument().getParagraphs().size() - 1));

In other words, I force a short delay before actually scrolling the screen.

Even though the above works, it's incredibly unsatisfactory, and there should be some way to do this the right way. However, I have been unable to figure it out.

I saw another question (Autoscroll JavaFX TextFlow) which suggested the use of layout() prior to scrolling. This does not remove the problem, although it does appear to make it less common.

Elias Mårtenson
  • 3,820
  • 23
  • 32
  • [mcve] please.. – kleopatra May 02 '22 at 05:01
  • That's a valid request. My current minimal example is too complicated and I don't want to subject people to it. The fact that the problem is intermittent also makes things worse. I'll keep trying to make something easy, but my assumption was that the question itself should be clear enough: There is a delay between updating a document and the internal model gets updated, and the question is how I can get notified when everything is in sync. – Elias Mårtenson May 02 '22 at 08:38
  • hmm .. from your description, I would expect the problem to be reproducible in the most simple ui (just the textArea and some means to add text in a way that doesn't force a layout), so writing it _should_ be simple enough .. but then those issues that are not simple to reproduce are the worst :) Good luck! – kleopatra May 02 '22 at 09:30
  • Thank you. You are right. I will write a new one that is hopefully simple enough. – Elias Mårtenson May 02 '22 at 14:06

0 Answers0