Solved

Attempt to mutate in notification exception

Posted on 2010-11-10
7
1,167 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
ID: 34106883
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
ID: 34107035
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
ID: 34107064
might be a job for a custom Document (that listens to the data store)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Author Comment

by:allelopath
ID: 34107168
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
ID: 34107297
a custom document for each text filed
0
 
LVL 1

Author Comment

by:allelopath
ID: 34113070
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
ID: 34116958
sounds about right
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 53
How to get all the API from website? 11 88
servlet example issue 6 38
Way to decrease size of apk file 9 67
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

770 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