DocumentListener dl = new MessageDocumentListener();
((AbstractDocument) nboxArea.getDocument()).setDocumentFilter(new DocumentFilter() {
public void insertStrin
You cannot modify the document inside the DocumentListener. Write a custom Document instead, which overrides the insertString() or remove() methods.
From Java Tutorials: How to write a DocumentListener
Document listeners should not modify the contents of the document; The change is already complete by the time the listener is notified of the change. Instead, write a custom document that overrides the insertString or remove methods, or both. See Listening for Changes on a Document for details.
If you want to mutate in the listener you can launch a separate thread to do it later with SwingUtilities.invokeLater. Be careful because the modifications from the separate thread will call the listener again, so set a boolean before launching the thread, return immediately from the listener if it is set and reset it after the modifications have been done in the separate thread.