From the Swing tutorial on text components:
You may want to change the document\'s text within a document listener. However, you should never modify t
A direct answer is using SwingUtilities.invokeLater() placing the Document modification code there. But mKorbel's answer (+1) covers most of the cases you can imagine.
SwingUtilities.invokeLater()
Document