Hello I want to update my JProgress Bar with the help of a Swing Worker class. I searched the other questions on this topic in this forum and took some code of a solution but in my case i does not work:( I have two classes: In the first class i create an object of the Swing Worker class BitmapFilterParralelGrey with this code:
Greyscale.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
int numThreads = Runtime.getRuntime().availableProcessors();
int HeightPerThread = image.getHeight() / numThreads;
BitmapFilterParallelGrey[] bitmapFilter = new BitmapFilterParallelGrey[numThreads];
int startRow = 0, endRow = 0;
for (int i = 0; i < numThreads; ++i) {
startRow = i * HeightPerThread;
endRow = startRow + HeightPerThread;
bitmapFilter[i] = new BitmapFilterParallelGrey(image,
startRow, endRow);
bitmapFilter[i].addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
if ("progress".equals(e.getPropertyName())) {
BitmapViewer.ProgressBar.setIndeterminate(false);
BitmapViewer.ProgressBar.setValue((Integer) e.getNewValue());
}
}
});
bitmapFilter[i].execute();
}
for (int i = 0; i < numThreads; i++) {
try {
bitmapFilter[i].get();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
showImage();
}
});
as the execution in the SwingWorker class shall be threaded i create several objects in the loop. In my second class BitmapFilterParallelGrey i filter an image in the doInBackground() and set the ProgressValue. The code:
public class BitmapFilterParallelGrey extends SwingWorker<Void, Void> {
BufferedImage image;
private int startRow, endRow;
public BitmapFilterParallelGrey(BufferedImage image, int startRow, int endRow){
this.image = image;
this.startRow= startRow;
this.endRow= endRow;
}
@Override
public Void doInBackground() throws Exception {
int width = image.getWidth();
for(int i=startRow; i<endRow; i++)
{
for(int j=0; j<width; j++)
{
//Filter image
}
setProgress(i/100);
Thread.sleep(10);
}
return null;
}
}
the Problem ist that the Progress Bar updated when the filtering is done (100%). Why doesnt it update while it filters in the background? Thanks!