Solved

Attempt to mutate in notification exception

Posted on 2010-11-10
7
1,159 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:allelopath
  • 4
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
looks like the event is getting triggered by a change in the text field
This would be better handled with a DocumentListener
0
 
LVL 1

Author Comment

by:allelopath
Comment Utility
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?

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
might be a job for a custom Document (that listens to the data store)
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Author Comment

by:allelopath
Comment Utility
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.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
Comment Utility
a custom document for each text filed
0
 
LVL 1

Author Comment

by:allelopath
Comment Utility
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?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
sounds about right
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now