The progress monitor does not show up and also edt gets stacked whnen I call doInBackground(). Anybody that could help me out I would really appreciate it.
mainFrame is running on the edt but is created in another class.
public void serializeAlbumsList(DefaultListModel<Album> albums) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
progressMonitor = new ProgressMonitor(mainFrame, "Please wait while saving your albums...", null, 0, 100);
}
});
Serializer serializer = new Serializer(albums);
try {
serializer.doInBackground();
} catch (Exception e) {
e.printStackTrace();
}
}
private class Serializer extends SwingWorker<Void, Void> implements PropertyChangeListener{
int progress;
private DefaultListModel<Album> albums;
public Serializer(DefaultListModel<Album> albums) {
this.albums = albums;
}
@Override
protected Void doInBackground() throws Exception {
setProgress(0);
for (int i = 0; i < albums.size(); i++) {
ObjectOutputStream os;
try {
FileOutputStream fs = new FileOutputStream("Album" + i + ".ser");
os = new ObjectOutputStream(fs);
os.writeObject(albums.getElementAt(i));
os.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
progress = i*(100/albums.size());
setProgress(progress);
}
return null;
}
@Override
public void propertyChange(PropertyChangeEvent e) {
if (progressMonitor.isCanceled()) {
cancel(true);
} else if ("progress" == e.getPropertyName()) {
int progress = ((Integer) e.getNewValue()).intValue();
progressMonitor.setProgress(progress);
}
}
}