Search code examples
javaswingjtextarea

Listen to printing text event on a custom console?


I have a repainting custom console in my swing application that prints xml text. I redirect all my println and log info statements to this console.

I need to unescape xml characters like & gt;, & lt;, etc. Instead of doing this in every place I use a println or log info, I want to do this unescaping on the jtextarea just before printing text in it. Is there any such listener or event to perform an action on the printing text just before the print action on the custom console?


Solution

  • You can add a document listener to the document of the text area to find the event of text changes, and then make another change yourself (which you will ignore inside your listeners) to replace the text.

    The wiser way however is probably going to be to implement your own document filter so that instead of listenening to events, you will modify only the text which is inserted/removed. For more information see Implementing a Document Filter.