title: Error with DocumentListener
body: <p>I have a JTextField that I want to be limited to fifteen characters. The problem is that when I type over 15 characters, it errors. How can i fix this? Do I have to use some other object?</p>
  
  <p>The error:
  Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification</p>
  
  <pre><code> final int maxNicknameLength = 15;
   final JTextField nickname = new JTextField(1); //Max length: 15
   DocumentListener docListen = new DocumentListener() {
        public void changedUpdate(DocumentEvent e) {
             lengthCheck(e, nickname, maxNicknameLength);
        }
  
        public void insertUpdate(DocumentEvent e) {
             lengthCheck(e, nickname, maxNicknameLength);
        }
  
        public void removeUpdate(DocumentEvent e) {
             lengthCheck(e, nickname, maxNicknameLength);
        }
        public void lengthCheck (DocumentEvent e, JTextField txt, int max) {
             if (txt.getText().length() &gt; max)
                  txt.setText(txt.getText().substring(0, max));
        }    
   };
   nickname.getDocument().addDocumentListener(docListen);
  </code></pre>