I created one external class with Swing worker that runs the progress bar. This is the code,
public class ExtProgressMonitor extends JFrame {
private static final long serialVersionUID = 1L;
private static final String s = "Database Statistics Report is exectuing in the Background";
private JProgressBar progressBar = new JProgressBar(0, 100);
private JLabel label = new JLabel(s, JLabel.CENTER);
public ExtProgressMonitor() {
this.setLayout(new GridLayout(0, 1));
this.setTitle("Database Utility Execution");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(progressBar);
this.add(label);
this.setSize(100, 100);
this.setLocationRelativeTo(null);
this.setVisible(true);
pack();
}
public void runCalc() {
progressBar.setIndeterminate(false);
progressBar.setStringPainted(false);
TwoWorker task = new TwoWorker();
task.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent e) {
if ("progress".equals(e.getPropertyName())) {
progressBar.setIndeterminate(false);
progressBar.setValue((Integer) e.getNewValue());
}
}
});
task.execute();
}
private class TwoWorker extends SwingWorker<Integer, Integer> {
private static final int N = 500;
private final DecimalFormat df = new DecimalFormat(s);
Integer x = 1;
@Override
protected Integer doInBackground() throws Exception {
if (!javax.swing.SwingUtilities.isEventDispatchThread()) {
System.out.println("javax.swing.SwingUtilities.isEventDispatchThread() + returned false.");
}
for (int i = 1; i <= N; i++) {
x = x - (((x * x - 2) / (2 * x)));
setProgress(i * (100 / N));
publish(Integer.valueOf(x));
Thread.sleep(1000); // simulate latency
}
return Integer.valueOf(x);
}
@Override
protected void process(List<Integer> chunks) {
for (Integer percent : chunks ) {
progressBar.setValue(progressBar.getValue() + percent);
}
}
}
The above code works when I call it in main class like below .
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ExtProgress t = new ExtProgress();
t.runCalc();
}
});
}
However When I try to call the same in my Action button that fetches a lot of row from database, takes around 15-20 minutes. progressbar gets launched, once the db process starts, the progressbar is frozen, while the db statistics is fetched.once the long process is over, the progress bar continues to run again.
private void jButton1ActionPerformed(java.awt.event.ActionEvent e) throws Exception {
ExtProgressMonitor t = new ExtProgressMonitor();
t.runCalc();
CheckStorage.DBVaultCheck(Host, port, instance, workbook, Schema_Password, schema);
//.. Rest of the process, check storage comes from another class.
});
Can you please help me fix this issue?