-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathProgressMonitorFrame.java
More file actions
114 lines (99 loc) · 3.1 KB
/
Copy pathProgressMonitorFrame.java
File metadata and controls
114 lines (99 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package progressMonitor;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* A frame that contains a button to launch a simulated activity and a text area for the activity
* output.
*/
class ProgressMonitorFrame extends JFrame
{
public static final int TEXT_ROWS = 10;
public static final int TEXT_COLUMNS = 40;
private Timer cancelMonitor;
private JButton startButton;
private ProgressMonitor progressDialog;
private JTextArea textArea;
private SimulatedActivity activity;
public ProgressMonitorFrame()
{
// this text area holds the activity output
textArea = new JTextArea(TEXT_ROWS, TEXT_COLUMNS);
// set up a button panel
JPanel panel = new JPanel();
startButton = new JButton("Start");
panel.add(startButton);
add(new JScrollPane(textArea), BorderLayout.CENTER);
add(panel, BorderLayout.SOUTH);
// set up the button action
startButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
startButton.setEnabled(false);
final int MAX = 1000;
// start activity
activity = new SimulatedActivity(MAX);
activity.execute();
// launch progress dialog
progressDialog = new ProgressMonitor(ProgressMonitorFrame.this,
"Waiting for Simulated Activity", null, 0, MAX);
cancelMonitor.start();
}
});
// set up the timer action
cancelMonitor = new Timer(500, new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (progressDialog.isCanceled())
{
activity.cancel(true);
startButton.setEnabled(true);
}
else if (activity.isDone())
{
progressDialog.close();
startButton.setEnabled(true);
}
else
{
progressDialog.setProgress(activity.getProgress());
}
}
});
pack();
}
class SimulatedActivity extends SwingWorker<Void, Integer>
{
private int current;
private int target;
/**
* Constructs the simulated activity that increments a counter from 0 to a
* given target.
* @param t the target value of the counter.
*/
public SimulatedActivity(int t)
{
current = 0;
target = t;
}
protected Void doInBackground() throws Exception
{
try
{
while (current < target)
{
Thread.sleep(100);
current++;
textArea.append(current + "\n");
setProgress(current);
}
}
catch (InterruptedException e)
{
}
return null;
}
}
}