I have the following code and just got ConcurrentModificationException.
fchProtocol = new FileChooser(lastFileLoc);
FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter("xml files (*.xml)", "xml");
fchProtocol.setFileFilter(xmlfilter); <<<< ***** exception from here
The exception trace info:
java.util.ConcurrentModificationException
java.util.Vector$Itr.checkForComodification(Vector.java:1184)
java.util.Vector$Itr.next(Vector.java:1137)
javax.swing.plaf.basic.BasicDirectoryModel$LoadFilesThread.cancelRunnables(BasicDirectoryModel.java:340)
javax.swing.plaf.basic.BasicDirectoryModel$LoadFilesThread.cancelRunnables(BasicDirectoryModel.java:346)
javax.swing.plaf.basic.BasicDirectoryModel.validateFileCache(BasicDirectoryModel.java:135)
javax.swing.plaf.basic.BasicDirectoryModel.propertyChange(BasicDirectoryModel.java:69)
java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:327)
java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
java.awt.Component.firePropertyChange(Component.java:8422)
javax.swing.JFileChooser.setFileFilter(JFileChooser.java:1473)
This is the Java API method code. It is the last line throws the exception.
public void setFileFilter(FileFilter filter) {
FileFilter oldValue = fileFilter;
fileFilter = filter;
if (filter != null) {
if (isMultiSelectionEnabled() && selectedFiles != null && selectedFiles.length > 0) {
Vector<File> fList = new Vector<File>();
boolean failed = false;
for (File file : selectedFiles) {
if (filter.accept(file)) {
fList.add(file);
} else {
failed = true;
}
}
if (failed) {
setSelectedFiles((fList.size() == 0) ? null : fList.toArray(new File[fList.size()]));
}
} else if (selectedFile != null && !filter.accept(selectedFile)) {
setSelectedFile(null);
}
}
firePropertyChange(FILE_FILTER_CHANGED_PROPERTY, oldValue, fileFilter);
}
This happened at GUI initialization stage. And the xmlfileter is a local variable.
I am using javaVersion = 1.8.0_20 in Linux. It is not reproducible and it only happened three times this year during my development process.
I don't know how to avoid this, or how to correctly use the FileChooser and its FileFilter. Anybody can help me?
EDIT:
It happened at the application initialization stage:
- main thread starts with main()
- initialized few non-GUI classes in the main thread;
- call
.
SwingUtilities.invokeAndWait(new Runnable()
public void run()
{
initalize GUI classes from here
}
);
to do GUI initialization, but failed in that GUI initialization thread at the first few steps. There is NO other thread inside the GUI initialization thread. And also the main thread has no action to access the GUI initialization thread.
Basic at that moment there are two threads: the main thread and the GUI initialization thread started via invorkAndWait() call.