How to get notified when changes in the document actually happens?

51 views Asked by At

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.

0

There are 0 answers