private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int jTableRows = jTable1.getRowCount();
ProgressMonitor progressMonitor;
progressMonitor = new ProgressMonitor(ImportDataFromExcel.this, "Running a Long Task", "", 0, jTableRows);
for (int i = 0; i < jTableRows; i++) {
String message = String.format("Completed %d.\n", i);
progressMonitor.setNote(message);
progressMonitor.setProgress(i);
}
}
When i click a button to insert the data in the database am expecting to get ProgressMonitor progress but am only getting the progress result when the whole process finishes. How can i view the progress in real time.
All heavy tasks in Swing should be executed by SwingWorkers. Otherwise, the big task will give hard time to the Event Dispatch Thread hence events cannot take place (GUI will freeze).
So, you have to create a
SwingWorkerand calculate the completed steps of the task in percentage and give the value to the progress bar. However, you have to have in mind that sinceprogressbar.setValue(int value)is a component update, it should only happen inside EDT. That's why you have to usepublishandprocessmethods of the worker.Let's see an example where we have to write 1000 lines to a text file in Desktop and see its progress. This is a "big task" (I sleep the thread), so it matches our case.