In last section, you have seen how to create and use an indeterminate progress
dialog. The ProgressDialog offers a convenient method for you to execute
task with an indeterminate progress dialog:
public static void execute(final Runnable task, Component parentComponent)
The execute method executes the specified task in a separate thread with a
progress dialog. It takes two parameters:
task - the task to be performed
parentComponent - UI component based on or null for console applications
Sample code:
1. final Runnable task = new Runnable() {
2. public void run() {
3. int range = 10;
4. for(int i=0; i<range; i++) {
5. System.out.println(i + " of " + range);
6. try {
7. Thread.sleep(300);
8. } catch (InterruptedException e) {
9. e.printStackTrace();
10. }
11. }
12. }
13. };
14. ...
15. JButton button = new JButton("Start");
16.
17. button.addActionListener(new ActionListener() {
18. public void actionPerformed(ActionEvent e) {
19. ProgressDialog.execute(task, frame);
20. }
21. });