Link to home
Start Free TrialLog in
Avatar of allelopath
allelopath

asked on

Attempt to mutate in notification exception

I'm getting an Attempt to mutate in notification exception. I have a class DataStore which holds data with a listener that takes action when data in it has changed. When it sets the data from the DataStore to a GUI component, the exception occurs. The data does make it to the component. This is a Swing thread issue of some sort, I guess.

How should I address this?

JTextField myTextField;
...
public void propertyChange(PropertyChangeEvent event) {
		
	InvisibleChangedComponent invisibleChangedComponent = InvisibleChangedComponent.getInstance();
	final Actions action = invisibleChangedComponent.getAction();

	if (action == Actions.CHANGED) {

	        final DataStore dataStore = DataStore.getInstance();
		String newString = dataStore.getMyString();
		try {
			myTextField.setText(newString);
		}
		catch (Exception e) {
			// Attempt to mutate in notification
		}
}

Open in new window

Avatar of Mick Barry
Mick Barry
Flag of Australia image

looks like the event is getting triggered by a change in the text field
This would be better handled with a DocumentListener
Avatar of allelopath
allelopath

ASKER

I see the problem, not apparent from the code I posted.
As stated, I have it listening to DataStore so that when DataStore changes, it is reflected in the test field. Also, however, I have a DocumentListener that listens for changes to the text field:

final Document myDocument = myTesxField.getDocument();
myDocument.addDocumentListener(documentListener);
myDocument.putProperty(NAME_KEY,MY_TEXTFIELD);

When changes are made to the textfield (by the user typing), these get pushed to the DataStore.
So it is cyclical and on the 2nd time around, it throws the exception.

So the question, how is it best to handle this situation, where I have a DataStore and a text field, and when either is updated, I want the data to go to the other?

might be a job for a custom Document (that listens to the data store)
ok, this textfield is one component of 5 on a JPanel (2 comboboxes and 2 more textfields), and they all behave the same way with respect to DataStore <-> component. Would I have 1 custom Document for each (ie 5 custom documents) or 1 custom Document for all? The component data can be changed individually.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So conceptually, I will have:
A Document and DocumentListener. When the data changes, either in the text field,or in the datasource, the change occurs to data held in the document. Then the text field and the data store are listening to the Document, and when it changes, the change it pushed to both.
Correct?
sounds about right