0

I have a display text area in my JavaFX app.

It has several methods for appending strings to it.

What I want is for any newly appended text to have some kind of feedback so the user is more visually aware of the new text being displayed.

Ideally what I would like is one of the following, but am very open to other suggestions:

  • The appended text is 'typed out' as if someone were typing it into the display text area itself. Or..
  • Some kind of highlighting of the new text as it appears.

Although again, I'm open to suggestions!

Thank you.

R.Sama
  • 103
  • 12
  • 2
    possible duplicate of http://stackoverflow.com/questions/9128535/highlighting-strings-in-javafx-textarea – Japu_D_Cret Mar 27 '17 at 13:50
  • Loop over the `chars` of the text. Insert one `char` in the `TextArea`, pause for some time, repeat... This would give an effect of typing – dumbPotato21 Mar 27 '17 at 13:55
  • Thanks, what is the standard way of doing this? I obviously don't want to use thread.sleep(); – R.Sama Mar 27 '17 at 14:48

2 Answers2

1

As suggested in Highlighting Strings in JavaFX TextArea, you can select the new text.

Assuming the text area is not editable, and you are only ever appending to the end of the existing text, you can do:

textArea.textProperty().addListener((obs, oldText, newText) -> {
    if (newText.length() > oldText.length()) {
        Platform.runLater(() ->
            textArea.selectRange(oldText.length(), newText.length()));
    }
});

If your text area is editable, or you are using it more generally, then you can do something similar each time you append text. For example:

String message = ... ;
textArea.positionCaret(textArea.getLength());
textArea.appendText(message);
textArea.extendSelection(textArea.getLength());
Community
  • 1
  • 1
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Hi, thanks for the reply - the listener would be ideal, but when placed directly into my initialise in the controller, it doesn't seem to have any effect. Can confirm the text display area is set to not editable. Would you have any suggestions? – R.Sama Mar 27 '17 at 17:56
  • 1
    It looks like something subsequently resets the caret, after the text change has been made. So a workaround is to wrap the selection call in a `Platform.runLater(...)` (though it feels like a bit of a hack). See update. – James_D Mar 27 '17 at 18:18
  • Perfect!! This now works. In this instance, since it's a toy project, I think I'm ok with a little hacky :) Thank you very much for this, +1! – R.Sama Mar 27 '17 at 18:40
0

Maybe try to use RichTextArea (https://github.com/TomasMikula/RichTextFX), then is many possibilites to show your text in different ways.

Sawior91
  • 23
  • 3