Issue
in my program I have to use JProgress Bar to show in real Time ,how many pixels are already encrypted ..so i was looking in internet and i found that i should use Thread , i tried but not working ,here is what I've done :
public class progress_barr extends Thread
{
public void run ()
{
JPB_crypt.setValue(prog); // prog is manipulated in showpixels function
System.err.println("progress here !");
}
}
public class calculate extends Thread
{
public void run()
{
showPixel();
}
}
than i call them later , this is how i use it
JPB_crypt.setValue(0);
appel=1; // to initiate show function
calculate calc = new calculate();
progress_barr pb = new progress_barr();
calc.start();
pb.start();
the problem still the same , my program finish data treatment than it set my jprogress to 100 % ..not in progressing but at once ! i need them to work simultaneously
Solution
Your general idea of using a thread is correct, but in order to be able to update the JProgressBar on the go with your calculation progress you need to use a SwingWorker.
The idea behind the SwingWorker is the fact that you can have methods running in the backgournd, by using the doInBackground method, which then publish your results to be used by the UI without locking it.
What you do in your bit of code is to actually have two threads that start at the same time. The first one calculates whatever is that you need to calculate and the second is supposed to update on the fly the progress bar.
This approach will not work, because each time the second thread will have to wait for the first one to end it's job in order to get the results.
You can read more about the SwingWorker here:
https://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html
Answered By - akortex