回答編集履歴

1

訂正。

2020/08/13 23:43

投稿

shiketa
shiketa

スコア3971

test CHANGED
@@ -6,33 +6,73 @@
6
6
 
7
7
 
8
8
 
9
+
10
+
9
11
  ```java
12
+
13
+ JButton button = new JButton("...");
14
+
15
+ JTextArea progress = new JTextArea();
16
+
17
+ button.addActionListener(ev -> {
18
+
19
+ while (true) {
20
+
21
+ // 重い処理
22
+
23
+ SwingUtilities.invokeLater(() -> {
24
+
25
+ progress.append("...");
26
+
27
+ });
28
+
29
+ }
30
+
31
+ });
32
+
33
+ ```
10
34
 
11
35
 
12
36
 
13
- new Thread() {
37
+ うえのように書いてもprogressは更新されません。ほかにもいろいろと問題がでてきます。重い処理をイベントディスパッチスレッド上で実行しているからです。
14
38
 
15
- @Override
39
+ progressをきちんと更新してほしければ、つぎのように書きます。
16
40
 
17
- public void run() {
18
41
 
19
- while (true) {
20
42
 
21
- // 重い処理
43
+ ```java
22
44
 
23
- SwingUtilities.invokeLater(() -> {
45
+ button.addActionListener(ev -> {
24
46
 
25
- textArea.append("...");
47
+ new Thread() {
26
48
 
49
+ @Override
50
+
51
+ public void run() {
52
+
53
+ while (true) {
54
+
55
+ // 重い処理
56
+
57
+ SwingUtilities.invokeLater(() -> {
58
+
59
+ progress.append("...");
60
+
27
- });
61
+ });
62
+
63
+ }
28
64
 
29
65
  }
30
66
 
31
- }
67
+ }.start();
32
68
 
33
- }.start();
69
+ });
34
70
 
35
71
  ```
72
+
73
+
74
+
75
+ ----
36
76
 
37
77
 
38
78