java.lang.IllegalStateException while using Document Listener in TextArea, Java

后端 未结 2 639
傲寒
傲寒 2020-12-06 11:30
DocumentListener dl = new MessageDocumentListener();
((AbstractDocument) nboxArea.getDocument()).setDocumentFilter(new DocumentFilter() {
    public void insertStrin         


        
相关标签:
2条回答
  • 2020-12-06 12:17

    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.

    0 讨论(0)
  • 2020-12-06 12:21

    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.

    0 讨论(0)
提交回复
热议问题