Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6569113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:35:09+00:00 2026-05-25T14:35:09+00:00

I have two JTextAreas in my GUI, and I have a DocumentListener on each

  • 0

I have two JTextAreas in my GUI, and I have a DocumentListener on each JTextArea, what I’m trying to do is for example when I type abc in text area number 1 it will take that document text modify it in some way and output it in the Document for JTextArea 2.

with my Listener I can get the source document I can get the text I can modify the text but when I try to put it back into the document I get an error

Exception in thread “AWT-EventQueue-0” java.lang.IllegalStateException: Attempt to mutate in notification

Please help.

Thanks

Here’s some code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Maxi
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class Test {

    static JFrame frame = new JFrame("CaesarEncipherGUI");
     static JPanel panel = new JPanel();
     static JTextArea area = new JTextArea(5,20);



     static JTextArea area1 = new JTextArea(5,20);


    static class MyDocumentListener2 implements DocumentListener {

    public void insertUpdate(DocumentEvent e) {
        updateLog(e,"");
    }
    public void removeUpdate(DocumentEvent e) {
        updateLog(e,"");

    }


    public void changedUpdate(DocumentEvent e) {

    }    


public void updateLog(DocumentEvent e, String action){


Document doc = (Document)e.getDocument();



try{


  System.out.println("Action detected  "+doc.getProperty("type"));

String text = doc.getText(0, doc.getLength());

doc.insertString(0, "hey", null); //heres the line that throws the error.



//mutation of text here

}catch (BadLocationException catchme2){}



}
}

        public static void main(String[] args){



            area.getDocument().addDocumentListener(new MyDocumentListener2());

         //initialize
         frame.setResizable(false);
         frame.setBounds(300, 300, 235, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



panel.add(area);
panel.add(area1);
frame.add(panel);
frame.setSize(235,400);
frame.setVisible(true);


     }



}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:35:10+00:00Added an answer on May 25, 2026 at 2:35 pm

    Likely you are trying to have the DocumentListener change the text on the same Document that it is listening to. This is not allowed as per the DocumentListener API which states:

    The DocumentEvent notification is based upon the JavaBeans event model. There is no guarantee about the order of delivery to listeners, and all listeners must be notified prior to making further mutations to the Document. This means implementations of the DocumentListener may not mutate the source of the event (i.e. the associated Document).

    One way around this is to place your method to change the Document’s text in a Runnable and to queue it on the EDT with SwingUtilities.invokeLater(...).

    Another solution, perhaps better, is to use a DocumentFilter.

    example with DocumentListener:

       static class MyDocumentListener2 implements DocumentListener {
          private boolean updating = false;
    
          public void insertUpdate(DocumentEvent e) {
             updateLog(e, "");
          }
    
          public void removeUpdate(DocumentEvent e) {
             updateLog(e, "");
    
          }
    
          public void changedUpdate(DocumentEvent e) {
    
          }
    
          public void updateLog(DocumentEvent e, String action) {
             if (updating) {
                return;
             }
             updating = true;
    
             final Document doc = (Document) e.getDocument();
    
             try {
    
                System.out.println("Action detected  " + doc.getProperty("type"));
    
                final String text = doc.getText(0, doc.getLength());
    
                SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                      try {
                         doc.insertString(0, "hey", null);
                         updating = false;
                      } catch (BadLocationException e) {
                         e.printStackTrace();
                      }
                   }
                });
    
             } catch (BadLocationException catchme2) {
                catchme2.printStackTrace();
             }
    
          }
       }
    

    And a DocumentListener and DocumentFilter example that turns all text to upper case:

    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
    
    public class Foo003 {
       private static final String ENTER = "enter";
    
       public static void main(String[] args) {
          final JTextArea myArea = new JTextArea(10, 20);
          final PlainDocument myDocument = (PlainDocument) myArea.getDocument();
    
          DocumentListener myDocumentListener = new DocumentListener() {
             private boolean changing = false;
    
             public void removeUpdate(DocumentEvent e) {}
    
             public void changedUpdate(DocumentEvent e) {
                toUpperCase(myArea, myDocument);
             }
    
             @Override
             public void insertUpdate(DocumentEvent e) {
                toUpperCase(myArea, myDocument);
             }
    
             private void toUpperCase(final JTextArea myArea,
                   final PlainDocument myDocument) {
                if (changing) {
                   return;
                }
                try {
                   changing = true;
                   final String text = myDocument
                         .getText(0, myDocument.getLength()).toUpperCase();
                   SwingUtilities.invokeLater(new Runnable() {
                      public void run() {
                         myArea.setText(text);
                         changing = false;
                      }
                   });
                } catch (BadLocationException e1) {
                   e1.printStackTrace();
                }
             }
    
          };
    
          myDocument.addDocumentListener(myDocumentListener);
    
          JOptionPane.showMessageDialog(null, new JScrollPane(myArea),
                "With DocumentListener", JOptionPane.INFORMATION_MESSAGE);
    
          myDocument.removeDocumentListener(myDocumentListener);
    
          myArea.setText("");
    
          myDocument.setDocumentFilter(new DocumentFilter() {
             @Override
             public void insertString(FilterBypass fb, int offset, String text,
                   AttributeSet attr) throws BadLocationException {
                text = text.toUpperCase();
                super.insertString(fb, offset, text, attr);
             }
    
             @Override
             public void replace(FilterBypass fb, int offset, int length,
                   String text, AttributeSet attrs) throws BadLocationException {
                text = text.toUpperCase();
                super.replace(fb, offset, length, text, attrs);
             }
          });
          JOptionPane.showMessageDialog(null, new JScrollPane(myArea),
                "With DocumentFilter", JOptionPane.INFORMATION_MESSAGE);
       }
    }
    

    A key difference between DocumentListeners and DocumentFilters (and someone correct me if I’m wrong!) is that DocumentListeners fire after the document has been updated while DocumentFilters fire before they have been updated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an input form with two textareas allowing a user to type in
I have a very simple Swing GUI with just a JTetxtArea. I am trying
I am trying to print unicode in a JTextArea. I have gotten it to
I have a JTextArea and am deteching if any text is selection, if none
I have JScrollPane with JTextArea inside it and I am trying to set the
I have two textareas. When the user clicks a text link, ie Add to
I have two applications written in Java that communicate with each other using XML
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two renderers. One of them extends JTextArea and another inherits JPanel. I
I have two paragraphs of text, one is saved in a file while the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.