TextEditor — Formatting is erased after setting up rich text


Imagine you use a TextEditor to display some text with formatting. You can do so using a couple of setFont() and insertTextAtCaret() calls. Usually this works fine.

However it doesn’t work if this happens while the text editor has focus. The change will often happen in response to some click which takes away that focus. What will happen is:

  • A button click (or whatever) event arrives
  • An asynchronous focus lost message is sent to the TextEditor
  • You change the text in whatever callback you have
  • The focus lost message arrives. This message will call updateValueFromText() (see juce_TextEditor.cpp 1) and erase all the formatting.

So now what — of course I can send another asynchronous message to put in the formatted text, but how do I make sure it arrives after the focus lost message?

1: that’s a link. For some reason links don’t have styles at all over here. Web designers never cease to surprise me (in bad ways)


Could you register a FocusChangeListener and format the text after the TextEditor has lost focus? You can see in the TextEditor::handleCommandMessage() method that the listeners will be called after the updateValueFromText() call.


Never mind, this behaviour was actually fixed a while ago. Time for an update.