The code below performs a long running task with a progress dialog showing its status:
1. import com.asprise.util.ui.progress.ProgressDialog;
2.
3. public class DemoSimple {
4. public static void main(String[] args) throws InterruptedException {
5. final int range = 10;
6. ProgressDialog progressDialog = new ProgressDialog(null, "Progress");
7. progressDialog.beginTask("Number Counting", range, true);
8.
9. for(int i=0; i<range; i++) {
10. if(progressDialog.isCanceled()) {
11. System.out.println("CANCELED.");
12. break;
13. }
14.
15. System.out.println(i + " of " + range);
16. progressDialog.worked(1);
17. Thread.sleep(500);
18. }
19.
20. System.exit(0);
21. }
22. }
com.asprise.util.ui.progress.ProgressDialog is the actually implementation of the Asprise Progress Monitor Dialog.
A progress dialog is created in Line 6:
ProgressDialog progressDialog = new ProgressDialog(null, "Progress");
The two parameters taken by the ProgressDialog constructor is:
parentComponent - the component that this dialog is based on or null for console applications.
title - the title of the dialog
Line 7 notifies the progress dialog the start of the task:
progressDialog.beginTask("Number Counting", range, true);
beginTask notifies the progress dialog the task name, the total amount of work to
be done, and whether the task can be cancelled or not. In our case,
the task name is ‘Number Counting’. The amount of work is range units, and this
task can be canceled.
The dialog will display a cancel button at the bottom since
this task can be canceled. In the code, we need to check whether the user has canceled the task or not by checking progressDialog.isCanceled(). If the user cancels the task, we stops the task. After certain amount of work has been completed, you notify the progress dialog with:
progressDialog.worked(1);
Do take note that this amount you pass to worked represents an installment, as opposed to a cumulative amount of work done to date. After all the work has been completed, the progress dialog closes automatically. You can also close it programmatically with progressDialog.finished().
When you the code, you should see the progress dialog shown similar to below: