-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathProgressDialog.java
More file actions
52 lines (45 loc) · 1.38 KB
/
ProgressDialog.java
File metadata and controls
52 lines (45 loc) · 1.38 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
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.ProgressMonitor;
import javax.swing.SwingWorker;
public class ProgressDialog implements PropertyChangeListener {
private ProgressMonitor pm;
private SwingWorker<Void, Void> t;
public static void make(final SwingWorker<Void, Void> task, final Component attach) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
show(task, attach);
}
});
}
private static void show(final SwingWorker<Void, Void> task, Component attach) {
ProgressDialog content = new ProgressDialog();
content.t = task;
content.run(attach);
}
private void run(Component attach) {
pm = new ProgressMonitor(attach, "", "Working...", 0, 100);
pm.setProgress(0);
t.addPropertyChangeListener(this);
t.execute();
}
public void propertyChange(PropertyChangeEvent e) {
if("progress" == e.getPropertyName()) {
pm.setProgress((Integer) e.getNewValue());
if(pm.isCanceled()) {
t.cancel(true);
}
}
if("state" == e.getPropertyName()
&& SwingWorker.StateValue.DONE == (SwingWorker.StateValue)e.getNewValue()) {
pm.close();
}
}
}