0

I want to make a documentListener that if I type in the JTextField : txt_ip4class and match regex of IPv4 format it will change the JComboBox : box_ip4class content.

If the content is changed (itemStateChanged), range of IP Class will be written down to JTextField : txt_rangeclass, and bit usage will be write down to `JTextField : txt_bitclass``*.

I have succeeded in this method, but with a classic method (using jButton), but I failed in this method using documentListener. Only JComboBox : box_ip4Class changed.

My code, applying documentListener to txt_ip4class:

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);
        }
});

classW.Main contains two conditions, if zero it will set other fields based on jComboBox (txt_ip4class will be replaced), if one it should be set jComboBox choosing an option. And from that option, it should do like option zero

Full code (I export these files from Netbeans project to zip):

  1. Succeed method which using jButton to execute
  2. Failed method which using documentListener without jButton

Full exception stacktrace is here

or

    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)

===============EDIT 1============

Thanks to @MarkRotteveel And this post

This problem is fixed, but another problem is exist.. Code in the doHighlight is running in a loop.

===============EDIT 2============

Updated the code. doHighlight no longer looping when the condition doesn't equal the regex. but still doing loop when the condition equals.

0 Answers0