I need to show the progress of a task in my project in these case the progress of the method ProcBaseCentral(jfSelectorArchivo.getSelectedFile()), in the progressbar. This method calls a sql query that takes a indefinitly time to finish.The indeterminate mode works for me, but i need to see some progress because is important to show the progress.
I managed to make good progress in the code, I can show the percentage but still have not found a way to measure progress. The progress bar doesn't finish at the same time of the method or function, I know that my code is wrong, I just don't find a way to measure the progress. Maybe someone can help me with an algorithm.
The next code is what i have so far.
private void bProcesar3ActionPerformed(java.awt.event.ActionEvent evt) {
final int N = 100;
bProcesar3.setEnabled(false);
class MyWorker extends SwingWorker<Double, Double> {
@Override
public Double doInBackground() throws InterruptedException {
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
Thread t = new Thread(new Runnable() {
public void run() {
ProcesarSOC(jfSelectorArchivo.getSelectedFile());
}
});
int cntr = 1; //
int cont = 0;
double d1;
double d2;
double d3;
t.start();
while (t.isAlive()) {
d1 = N;
d2 = cont++;
d3 = d2 / d1;
d3 = d3 * 100;
if (d3 >= cntr) {
cntr++;
setProgress(cntr);
publish(Double.valueOf(cntr));
}
}
return Double.valueOf(0);
}
@Override
protected void process(List<Double> chunks) {
for (double d : chunks) {
System.out.println(": " + d);
}
}
@Override
public void done() {
Toolkit.getDefaultToolkit().beep();
setCursor(null);
}
}
MyWorker task = new MyWorker();
task.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
if ("progress".equals(e.getPropertyName())) {
jProgressBar1.setIndeterminate(false);
jProgressBar1.setValue((Integer) e.getNewValue());
System.out.println("**: " + e.getNewValue());
}
}
});
task.execute();
}
Thanks for your time.