问题
I have code which I have been using for years and this morning I noticed property change isn't being called when the task is done. I've got the swing worker set up as an inner class and I put a break point on the String properyName = evt..... and it never hits the break point.
void loadData() {
work2 = new bkgdLoadData();
work2.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
String propertyName = evt.getPropertyName();
if( propertyName.equals("state")) {
SwingWorker.StateValue state = (SwingWorker.StateValue) evt.getNewValue();
if( state == SwingWorker.StateValue.DONE) {
work2 = null;
}
}
}
});
work2.execute();
}
You can see that I set the object work2 to null when the task is finished and now it is no longer being set to null. In the class I added a done routine which it hits when the doinbackground is finished. What is puzzling me is why the property change listener isn't triggered. Something must have changed without my noticing.
protected class bkgdLoadData extends SwingWorker<Integer, Object> {
@Override
protected Integer doInBackground() {
switch(bkgdMode) {
case 0:
doRead();
break;
case 1:
doWrite();
break;
case 2:
runRobot();
break;
}
return 0;
}
@Override
protected void done() {
int i=0;
i++;
}
}
The breakpoint at done is hit but no property change notice is delivered. (I put the done routine for the sole purpose of verifying that the swing worker knows that it is done.)
I looked at the documentation and I don't see that I have to manually fire off some sort of property change, so I am really, really stuck and would appreciate another pair of eyes to tell me what stupid mistake I am mistaking.
Thanks, Ilan
回答1:
It turned out my Java was corrupted. Removing JDK 1.6 and reinstalling it from the repository wasn't good enough.
My links in Netbeans to 1.6 got damamged and I had to reinstall Netbeans as well (going over to 7.3.1 in the process). Netbeans would not recognize the repository JDK 1.6 as valid so I had to go to Oracle and get the original version. Netbeans recognized the original and the problem I reported above was no longer a problem.
I removed the void done() routine since it had no purpose other than a place to put a break point. The code as such is OK. Thanks for the help.
来源:https://stackoverflow.com/questions/18041687/in-swing-worker-propertychange-isnt-being-called