-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathProgressBarFrame.java
More file actions
112 lines (98 loc) · 2.84 KB
/
Copy pathProgressBarFrame.java
File metadata and controls
112 lines (98 loc) · 2.84 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
package progressBar;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
/**
* A frame that contains a button to launch a simulated activity, a progress bar, and a text area
* for the activity output.
*/
public class ProgressBarFrame extends JFrame
{
public static final int TEXT_ROWS = 10;
public static final int TEXT_COLUMNS = 40;
private JButton startButton;
private JProgressBar progressBar;
private JCheckBox checkBox;
private JTextArea textArea;
private SimulatedActivity activity;
public ProgressBarFrame()
{
// this text area holds the activity output
textArea = new JTextArea(TEXT_ROWS, TEXT_COLUMNS);
// set up panel with button and progress bar
final int MAX = 1000;
JPanel panel = new JPanel();
startButton = new JButton("Start");
progressBar = new JProgressBar(0, MAX);
progressBar.setStringPainted(true);
panel.add(startButton);
panel.add(progressBar);
checkBox = new JCheckBox("indeterminate");
checkBox.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
progressBar.setIndeterminate(checkBox.isSelected());
progressBar.setStringPainted(!progressBar.isIndeterminate());
}
});
panel.add(checkBox);
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);
activity = new SimulatedActivity(MAX);
activity.execute();
}
});
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++;
publish(current);
}
}
catch (InterruptedException e)
{
}
return null;
}
protected void process(List<Integer> chunks)
{
for (Integer chunk : chunks)
{
textArea.append(chunk + "\n");
progressBar.setValue(chunk);
}
}
protected void done()
{
startButton.setEnabled(true);
}
}
}