I have written a program which uses the progressbar class from the default java runtime. It works perfectly except for the fact that it only works once. I use it to display the loading of various images into my GUI and it works fine on startup. However, once I call it again it just displays a JFrame with no content. I can drag the JFrame around and everything, it's just that it won't display anything. This is peculiar considering it works when it runs the first time, but not there after. Here is my code:
public void prefetch (){
try{
pageURLs = mangaEngine.getPageList();
monitor = new ProgressMonitor(parent, "Loading Chapter..,", null, 0, mangaEngine.getPageList().length);
pages = new StretchIconHQ[pageURLs.length];
//Resets page counter to first page by fetching pages in reverse order
monitor.setMaximum(pageURLs.length-1);
for(int i = pageURLs.length-1; i>=0; i--){
pages[i] = mangaEngine.loadImg(pageURLs[i]);
monitor.setProgress(pageURLs.length-1-i);
monitor.setNote("Loading Image:" + (pageURLs.length-1-i) + " of " + (pageURLs.length-1));
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
Any ideas on how to fix it?
You need to run the long-running code in a background thread such as that created by a SwingWorker.
In fact, the Progress Monitor Tutorial describes how to do this exactly: