Class ProgressDialog<T,V>

Type Parameters:
T - the result type returned by SwingWorker's doInBackground and get methods
V - the type used for carrying out intermediate results by SwingWorker's publish and process methods
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, RootPaneContainer, WindowConstants

public class ProgressDialog<T,V> extends JDialog
A dialog window that shows the progress of a long-running task.

Before using this dialog, a SwingWorker task class has to be implemented that stores the reference to this ProgressDialog, e.g.,

 public class Task extends SwingWorker<Void, Voide> {
    private final ProgressDialog progressDialog;
    
    public Task(ProgressDialog progressDialog) {
        this.progressDialog = progressDialog;
    }
 
    // other code
 }
 
Either the doInBackground() or process(...) method of the Task has to call progressDialog.setValue(val) to update the progress bar accordingly.

Once the task and progress dialog are instantiated, it is necessary to attach listener that reacts to the end of the task computation, e.g.,

 ProgressDialog progressDialog = new ProgressDialog(null);
 Task task = new Task(progressDialog);
        
 task.addPropertyChangeListener((PropertyChangeEvent evt) -> {
    if ("state".equals(evt.getPropertyName()) && (SwingWorker.StateValue.DONE.equals(evt.getNewValue()))) {
        // code invoked when the task is done
    }
 });
 
Other states than the task end can be handled in similar way.

Finally, the task is executed in separate thread:

 progressDialog.runTask(task);
 
See Also:
  • Constructor Details

    • ProgressDialog

      public ProgressDialog(Component comp, String label)
      Constructor.
      Parameters:
      comp - the component in relation to which the dialog window location is determined
      label - label of the dialog window
  • Method Details

    • runTask

      public void runTask(SwingWorker<T,V> task)
      Runs the task in the separate thread and displays the progress dialog.
      Parameters:
      task - Task to be run
    • setValue

      public void setValue(int progress)
      Updates progress bar. Should be executed from the task
      Parameters:
      progress - Value in the range 0 .. 100