-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwingWorkerTest.java
144 lines (124 loc) · 4.66 KB
/
SwingWorkerTest.java
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package swingWorker;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
/**
* This program demonstrates a worker thread that runs a potentially time-consuming task.
*
* @author Cay Horstmann
* @version 1.11 2015-06-21
*/
public class SwingWorkerTest {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame frame = new SwingWorkerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
/**
* This frame has a text area to show the contents of a text file, a menu to open a file and
* cancel the opening process, and a status line to show the file loading progress.
*/
class SwingWorkerFrame extends JFrame {
public static final int TEXT_ROWS = 20;
public static final int TEXT_COLUMNS = 60;
private JFileChooser chooser;
private JTextArea textArea;
private JLabel statusLine;
private JMenuItem openItem;
private JMenuItem cancelItem;
private SwingWorker<StringBuilder, ProgressData> textReader;
public SwingWorkerFrame() {
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
textArea = new JTextArea(TEXT_ROWS, TEXT_COLUMNS);
add(new JScrollPane(textArea));
statusLine = new JLabel(" ");
add(statusLine, BorderLayout.SOUTH);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("File");
menuBar.add(menu);
openItem = new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(event -> {
// show file chooser dialog
int result = chooser.showOpenDialog(null);
// if file selected, set it as icon of the label
if (result == JFileChooser.APPROVE_OPTION) {
textArea.setText("");
openItem.setEnabled(false);
textReader = new TextReader(chooser.getSelectedFile());
textReader.execute();
cancelItem.setEnabled(true);
}
});
cancelItem = new JMenuItem("Cancel");
menu.add(cancelItem);
cancelItem.setEnabled(false);
cancelItem.addActionListener(event -> textReader.cancel(true));
pack();
}
private static class ProgressData {
public int number;
public String line;
}
private class TextReader extends SwingWorker<StringBuilder, ProgressData> {
private File file;
private StringBuilder text = new StringBuilder();
public TextReader(File file) {
this.file = file;
}
// The following method executes in the worker thread; it doesn't touch Swing components.
@Override
public StringBuilder doInBackground() throws IOException, InterruptedException {
int lineNumber = 0;
try (Scanner in = new Scanner(new FileInputStream(file), "UTF-8")) {
while (in.hasNextLine()) {
String line = in.nextLine();
lineNumber++;
text.append(line).append("\n");
ProgressData data = new ProgressData();
data.number = lineNumber;
data.line = line;
publish(data);
Thread.sleep(1); // to test cancellation; no need to do this in your programs
}
}
return text;
}
// The following methods execute in the event dispatch thread.
@Override
public void process(List<ProgressData> data) {
if (isCancelled()) return;
StringBuilder b = new StringBuilder();
statusLine.setText("" + data.get(data.size() - 1).number);
for (ProgressData d : data) b.append(d.line).append("\n");
textArea.append(b.toString());
}
@Override
public void done() {
try {
StringBuilder result = get();
textArea.setText(result.toString());
statusLine.setText("Done");
} catch (InterruptedException ignored) {
} catch (CancellationException ex) {
textArea.setText("");
statusLine.setText("Cancelled");
} catch (ExecutionException ex) {
statusLine.setText("" + ex.getCause());
}
cancelItem.setEnabled(false);
openItem.setEnabled(true);
}
}
}