-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvokeLaterDemo.java
More file actions
48 lines (39 loc) · 1.09 KB
/
Copy pathInvokeLaterDemo.java
File metadata and controls
48 lines (39 loc) · 1.09 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
package threadbook.ch09;
import java.awt.*;
import javax.swing.*;
public class InvokeLaterDemo extends Object {
private static void print(String msg) {
String name = Thread.currentThread().getName();
System.out.println(name + ": " + msg);
}
public static void main(String[] args) {
final JLabel label = new JLabel("--------");
JPanel panel = new JPanel(new FlowLayout());
panel.add(label);
JFrame f = new JFrame("InvokeLaterDemo");
f.setContentPane(panel);
f.setSize(300, 100);
f.setVisible(true);
try {
print("sleeping for 3 seconds");
Thread.sleep(3000);
} catch ( InterruptedException ix ) {
print("interrupted while sleeping");
}
print("creating code block for event thread");
Runnable setTextRun = new Runnable() {
public void run() {
try {
Thread.sleep(100); // for emphasis
print("about to do setText()");
label.setText("New text!");
} catch ( Exception x ) {
x.printStackTrace();
}
}
};
print("about to invokeLater()");
SwingUtilities.invokeLater(setTextRun);
print("back from invokeLater()");
}
}