I am programming a little tool to apply some filters to a large amount of images. I want to add an progressbar while loading in images. For this i've written this simple code here:
//Set up Progressbar
int pictureCount = 0;
for (File file : chooser.getSelectedFile().listFiles(isImage)) {
pictureCount++;
}
progressBar.setMinimum(0);
progressBar.setMaximum(pictureCount);
//Load all the Images and updating Progressbar
for (File file : chooser.getSelectedFile().listFiles(isImage)) {
BufferedImage bi = ImageIO.read(file);
tilesNew.add(bi);
progressBar.setValue(progressBar.getValue() + 1);
}
Unfortunately the progressbar is only updated at the end of the loop. so it shows 0% the whole time and then directly 100% after the program has loaded all images for a few seconds.
So far I have tried to use
frame.revalidate()
, frame.repaint()
and Thread.sleep()
in the loop. All without success.
I haven't used Swing for a long time, but it seems that you are doing something wrong commonly found in any platform with GUI.
Your code seems to read large files in the main (UI) thread.
The UI will be updated when the main thread is not busy. However, the main thread will be always busy while it is processing the image.
You should create a new Thread
, which processes the files and the images, and then the created thread should pass it to the main thread to update the UI every time the processBar
needs to be updated.
Here is some helpers in Swing
to support multi-threading:
https://stackabuse.com/how-to-use-threads-in-java-swing/