-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalanceLookup.java
More file actions
190 lines (152 loc) · 4.34 KB
/
Copy pathBalanceLookup.java
File metadata and controls
190 lines (152 loc) · 4.34 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package threadbook.ch09;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BalanceLookup extends JPanel {
private JTextField acctTF;
private JTextField pinTF;
private JButton searchB;
private JButton cancelB;
private JLabel balanceL;
private volatile Thread lookupThread;
public BalanceLookup() {
buildGUI();
hookupEvents();
}
private void buildGUI() {
JLabel acctL = new JLabel("Account Number:");
JLabel pinL = new JLabel("PIN:");
acctTF = new JTextField(12);
pinTF = new JTextField(4);
JPanel dataEntryP = new JPanel();
dataEntryP.setLayout(new FlowLayout(FlowLayout.CENTER));
dataEntryP.add(acctL);
dataEntryP.add(acctTF);
dataEntryP.add(pinL);
dataEntryP.add(pinTF);
searchB = new JButton("Search");
cancelB = new JButton("Cancel Search");
cancelB.setEnabled(false);
JPanel innerButtonP = new JPanel();
innerButtonP.setLayout(new GridLayout(1, -1, 5, 5));
innerButtonP.add(searchB);
innerButtonP.add(cancelB);
JPanel buttonP = new JPanel();
buttonP.setLayout(new FlowLayout(FlowLayout.CENTER));
buttonP.add(innerButtonP);
JLabel balancePrefixL = new JLabel("Account Balance:");
balanceL = new JLabel("BALANCE UNKNOWN");
JPanel balanceP = new JPanel();
balanceP.setLayout(new FlowLayout(FlowLayout.CENTER));
balanceP.add(balancePrefixL);
balanceP.add(balanceL);
JPanel northP = new JPanel();
northP.setLayout(new GridLayout(-1, 1, 5, 5));
northP.add(dataEntryP);
northP.add(buttonP);
northP.add(balanceP);
setLayout(new BorderLayout());
add(northP, BorderLayout.NORTH);
}
private void hookupEvents() {
searchB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
search();
}
});
cancelB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cancelSearch();
}
});
}
private void search() {
// better be called by event thread!
ensureEventThread();
searchB.setEnabled(false);
cancelB.setEnabled(true);
balanceL.setText("SEARCHING ...");
// get a snapshot of this info in case it changes
String acct = acctTF.getText();
String pin = pinTF.getText();
lookupAsync(acct, pin);
}
private void lookupAsync(String acct, String pin) {
// Called by event thread, but can be safely
// called by any thread.
final String acctNum = acct;
final String pinNum = pin;
Runnable lookupRun = new Runnable() {
public void run() {
String bal = lookupBalance(acctNum, pinNum);
setBalanceSafely(bal);
}
};
lookupThread = new Thread(lookupRun, "lookupThread");
lookupThread.start();
}
private String lookupBalance(String acct, String pin) {
// Called by lookupThread, but can be safely
// called by any thread.
try {
// Simulate a lengthy search that takes 5 seconds
// to communicate over the network.
Thread.sleep(5000);
// result "retrieved", return it
return "1,234.56";
} catch ( InterruptedException x ) {
return "SEARCH CANCELLED";
}
}
private void setBalanceSafely(String newBal) {
// Called by lookupThread, but can be safely
// called by any thread.
final String newBalance = newBal;
Runnable r = new Runnable() {
public void run() {
try {
setBalance(newBalance);
} catch ( Exception x ) {
x.printStackTrace();
}
}
};
SwingUtilities.invokeLater(r);
}
private void setBalance(String newBalance) {
// better be called by event thread!
ensureEventThread();
balanceL.setText(newBalance);
cancelB.setEnabled(false);
searchB.setEnabled(true);
}
private void cancelSearch() {
// better be called by event thread!
ensureEventThread();
cancelB.setEnabled(false); // prevent additional requests
if ( lookupThread != null ) {
lookupThread.interrupt();
}
}
private void ensureEventThread() {
// throws an exception if not invoked by the
// event thread.
if ( SwingUtilities.isEventDispatchThread() ) {
return;
}
throw new RuntimeException("only the event " +
"thread should invoke this method");
}
public static void main(String[] args) {
BalanceLookup bl = new BalanceLookup();
JFrame f = new JFrame("Balance Lookup");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setContentPane(bl);
f.setSize(400, 150);
f.setVisible(true);
}
}