Bick
Bick

Reputation: 18531

java attempt to mutate in notification

I have two swing ui forms and a module that they both look at.
Each ui is adding a listener to the change of an attribute and update its own textfield when a change occurs.

basiccaly - they both should update the module and be update from it. Is there a way simple to do it whithout a binding framework

Here is how I do it (but I keep getting attempt to mutate in notification ) -

On the update of my textField

 myTextField.getDocument().addDocumentListener(new TFDocumentListener() {
            protected void userChangedTF() {
                Float value = myTextField.getValue();
                if (value != null) {
                    myObj.setMyAttribute(value);
                }
            }
        });

still in the ui - registering the change

        myObj.addMyAttributeChangedListener(new ValueChangeListener<Float>() {
            @Override public void valueChanged(Float value) {
                if (!myTextField.isFocusOwner()) {
                    myTextField.setValueIn(value);
                }
            }
        });

in the module - when setMyAttribute occurs - it calls this function

private void notifyIntervalChanged(float newValue) {
    for (ValueChangeListener valueChangeListener : intervalChangedListenersList) {
        valueChangeListener.valueChanged(newValue);
    }
}

and I declared

public interface ValueChangeListener<T> {
     void valueChanged(T Value)
}

Upvotes: 3

Views: 6291

Answers (1)

StanislavL
StanislavL

Reputation: 57421

If you need to change content of the same JTextComponent in the listener wrap the change (e.g. setText()) in the SwingUtilities.invokeLater()

Upvotes: 14

Related Questions