How to get a notification when the textEditor’s text is changed by the user

how can i get the textEditor’s text dynaticly?
I use a float number 0.5 as the textEditor’s default text, then i want to get the new text as soon as the text is changed while the program is running .

is a good place to start.

First of all you might want to consider to chose a title for your posts that is a bit more meaningful than “JUCE function” which could be nearly everything. How about “How to get a notification when the textEditor’s text is changed by the user”?

Now regarding your question, the best place to look for answers yourself is always the JUCE API documentation that you’ll find here:

If you go to the documentation for the TextEditor class here you’ll see that the TextEditor class has a nested TextEdior::Listener class that says TextEdior::Listener: Receives callbacks from a TextEditor component when it changes.

So this is what you are looking for. You see that all of its member functions are marked virtual. This means that you’ll need to create your own unique class that handles what happens if one of this functions is called. Let’s just create a listener that prints the changes to stdout:

class MyTextEditorListener : public TextEditor::Listener
    void textEditorTextChanged (TextEditor& textEditor) override 
        std::cout << textEditor.getText() << std::endl;

Now you need to create an instance of your listener beneath the instance of your TextEditor and tell the TextEditor to report the changes to your listener. This could look like that:

MyTextEditorListener listener;
TextEditor editor;

//... somewhere in the setup code where you also set default text and make it visible, etc...
editor.addListener (&listener);

Now everything should work. Keep in mind that your listener instance must not be deleted before your editor as this might cause bugs related to the editor calling a listener that does not exist anymore.


What is the best practice for this? I tend to always add a corresponding removeListener for every addListener, but is it OK to just delete the listening object before the listener and not worry about the removeListener in that case?

Yes, i had read this part this afternoon,but not know how to use addListener and removeListener appropritely.Maybe i need to do some tests.

Ok, thank you ,i will remember that next time when i propose the question.