title: [IllegalStateException : Attempt to Mutate in Notification]
body: <p>I want to make a <code>documentListener</code> that <em>if I type in the <code>JTextField : txt_ip4class</code> and match regex of IPv4 format</em> it will change the <code>JComboBox : box_ip4class</code> content.</p>
  
  <p>If the content is changed (<code>itemStateChanged</code>), range of IP Class will be written down to <code>JTextField : txt_rangeclass</code>, <em>and</em> bit usage will be write down to `JTextField : txt_bitclass``*.</p>
  
  <p>I have succeeded in this method, but with a classic method <em>(using <code>jButton</code>)</em>, but I failed in this method using <code>documentListener</code>. Only <code>JComboBox : box_ip4Class</code> changed.</p>
  
  <p>My code, applying <code>documentListener</code> to <code>txt_ip4class</code>:</p>
  
  <pre><code>txt_ip4class.getDocument().addDocumentListener(new DocumentListener() {
      @Override
      public void insertUpdate(DocumentEvent e) {
          logUpdate();
      }
  
      @Override
      public void removeUpdate(DocumentEvent e) {
          logUpdate();
      }
  
      @Override
      public void changedUpdate(DocumentEvent e) {
          logUpdate();
      }
  
      void logUpdate() {
          Runnable doHighlight = new Runnable() {
                  @Override
                  public void run() {
                      String aTxt = txt_ip4class.getText();
                      classValue = 1;
                      if (!aTxt.matches("\\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}\\b")) {
                          classValue = 0;
                      } else if (aTxt.matches("\\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}\\b")) {
                          try {
                              classW.classMain(classValue, box_ip4class, txt_ip4class, txt_rangeclass, txt_bitclass);
  
                          } catch (IllegalStateException e) {
  
                          }
                      }
                  }
              };
              SwingUtilities.invokeLater(doHighlight);
          }
  });
  </code></pre>
  
  <p><code>classW.Main</code> contains two conditions, if zero it will set other fields based on <code>jComboBox</code> (<code>txt_ip4class</code> will be replaced), if one it should be set <code>jComboBox</code> choosing an option. And from that option, it should do like option zero</p>
  
  <p>Full code (I export these files from Netbeans project <em>to zip</em>):</p>
  
  <ol>
  <li><a href="https://drive.google.com/file/d/1GLdIdzmZCB6DZXg_ga5t34gRLQO1Lj6X/view?usp=sharing" rel="nofollow noreferrer" title="NetworkingTools 0.095">Succeed method which using jButton to execute</a></li>
  <li><a href="https://drive.google.com/file/d/1FGaORt9mq9NdjPTV7DG2OtOxxiPULuuv/view?usp=sharing" rel="nofollow noreferrer" title="NetworkingTools 0.096">Failed method which using documentListener without jButton</a></li>
  </ol>
  
  <p>Full exception stacktrace is <a href="https://pastebin.com/0s90ddre" rel="nofollow noreferrer">here</a></p>
  
  <p>or </p>
  
  <pre><code>    Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification
      at javax.swing.text.AbstractDocument.writeLock(AbstractDocument.java:1338)
      at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:658)
      at javax.swing.text.JTextComponent.setText(JTextComponent.java:1669)
      at engines.ClassW.setFromCombo(ClassW.java:140)
      at interfaces.Netgui$1.logUpdate(Netgui.java:63)
      at interfaces.Netgui$1.insertUpdate(Netgui.java:41)
      at javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.java:201)
      at javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument.java:748)
      at javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:707)
      at javax.swing.text.PlainDocument.insertString(PlainDocument.java:130)
      at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:669)
      at javax.swing.text.JTextComponent.replaceSelection(JTextComponent.java:1328)
      at javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerformed(DefaultEditorKit.java:884)
      at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1663)
      at javax.swing.JComponent.processKeyBinding(JComponent.java:2882)
      at javax.swing.JComponent.processKeyBindings(JComponent.java:2929)
      at javax.swing.JComponent.processKeyEvent(JComponent.java:2845)
      at java.awt.Component.processEvent(Component.java:6310)
      at java.awt.Container.processEvent(Container.java:2236)
      at java.awt.Component.dispatchEventImpl(Component.java:4889)
      at java.awt.Container.dispatchEventImpl(Container.java:2294)
      at java.awt.Component.dispatchEvent(Component.java:4711)
      at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
      at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
      at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
      at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
      at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
      at java.awt.Component.dispatchEventImpl(Component.java:4760)
      at java.awt.Container.dispatchEventImpl(Container.java:2294)
      at java.awt.Window.dispatchEventImpl(Window.java:2746)
      at java.awt.Component.dispatchEvent(Component.java:4711)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
      at java.awt.EventQueue.access$500(EventQueue.java:97)
      at java.awt.EventQueue$3.run(EventQueue.java:709)
      at java.awt.EventQueue$3.run(EventQueue.java:703)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
      at java.awt.EventQueue$4.run(EventQueue.java:731)
      at java.awt.EventQueue$4.run(EventQueue.java:729)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
      BUILD SUCCESSFUL (total time: 19 seconds)
  </code></pre>
  
  <p>===============EDIT 1============</p>
  
  <p>Thanks to @MarkRotteveel
  And <a href="https://stackoverflow.com/questions/48115180/illegalstateexception-attempt-to-mutate-in-notification" title="Maybe the same problem">this post</a></p>
  
  <p>This problem is fixed, but another problem is exist..
  Code in the <code>doHighlight</code> is running in a loop.</p>
  
  <p>===============EDIT 2============</p>
  
  <p>Updated the code. 
  doHighlight no longer looping when the condition doesn't equal the regex.
  but still doing loop when the condition equals.</p>