Progress Monitor Dialog - an Improved Java Swing ProgressMonitor for long running process

Displaying Sub Task Progress

Index

Adding a sub task progress indicator in ProgressDialog is a breeze. We modifies the code in (1) and add the part highlighted in bold font to display a sub task progress:

1.	public class DemoSubTask {
2.	
3.		public static void main(String[] args) throws InterruptedException {
4.			final int range = 10;
5.	
6.			try {
7.				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
8.			} catch (Exception e) {
9.				e.printStackTrace();
10.			}		
11.			
12.			ProgressDialog progressDialog = new ProgressDialog(null, "Progress");
13.			progressDialog.beginTask("Number Counting", range, true);
14.			
15.			for(int i=0; i<range; i++) {
16.				if(progressDialog.isCanceled()) {
17.					System.out.println("CANCELED.");
18.					break;
19.				}
20.	
21.				System.out.println(i + " of " + range);
22.				
23.				progressDialog.beginSubTask("Counting #" + i, 5);
24.				for(int j=0; j<5; j++) {
25.					progressDialog.subWorked(1);
26.					Thread.sleep(100);
27.				}
28.				
29.				progressDialog.worked(1);
30.				Thread.sleep(500);
31.			}
32.			
33.			System.exit(0);
34.		}
35.	}

The corresponding progress dialog shown is illustrated below:



  

ALL RIGHTS RESERVED BY JACK LI © 1998 - 2010. Terms of Use | Privacy Policy