-
Notifications
You must be signed in to change notification settings - Fork 1
/
chapter20.java
586 lines (453 loc) · 15.1 KB
/
chapter20.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// A very simple JOptionPane demonstration.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MsgDialogDemo {
JLabel jlab;
JButton jbtnShow;
JFrame jfrm;
MsgDialogDemo() {
// Create a new JFrame container.
jfrm = new JFrame("Simple Message Dialog");
// Specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(400, 250);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a label that will show when the dialog returns.
jlab = new JLabel();
// Create a button that will display the dialog.
jbtnShow = new JButton("Show Dialog");
// Add action listener for the button.
jbtnShow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jlab.setText("Dialog Opened");
// Create a dialog that shows a message.
JOptionPane.showMessageDialog(jfrm,
"Disk space is low.");
// This statement won't execute until the
// call to showMessageDialog() returns.
jlab.setText("Dialog Closed");
}
});
// Add the button and label to the content pane.
jfrm.add(jbtnShow);
jfrm.add(jlab);
// Display the frame.
jfrm.setVisible(true);
}
public static void main(String[] args) {
// Create the GUI on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MsgDialogDemo();
}
});
}
}
// -----------------------------------------
// Use a simple confirmation dialog.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ConfirmDialogDemo {
JLabel jlab;
JButton jbtnShow;
JFrame jfrm;
ConfirmDialogDemo() {
// Create a new JFrame container.
jfrm = new JFrame("A Confirmation Dialog");
// Specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(400, 250);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a label that will show the user's response.
jlab = new JLabel();
// Create button that will display the dialog.
jbtnShow = new JButton("Show Dialog");
// Add action listener for the button.
jbtnShow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
// Create a confirmation dialog.
int response = JOptionPane.showConfirmDialog(
jfrm,
"Remove unused files?");
// Show the response.
switch(response) {
case JOptionPane.YES_OPTION:
jlab.setText("You answered Yes.");
break;
case JOptionPane.NO_OPTION:
jlab.setText("You answered No.");
break;
case JOptionPane.CANCEL_OPTION:
jlab.setText("Cancel pressed.");
break;
case JOptionPane.CLOSED_OPTION:
jlab.setText("Dialog closed without response.");
break;
}
}
});
// Add the button and label to the content pane.
jfrm.add(jbtnShow);
jfrm.add(jlab);
// Display the frame.
jfrm.setVisible(true);
}
public static void main(String[] args) {
// Create the GUI on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ConfirmDialogDemo();
}
});
}
}
// -----------------------------------------
// A simple input dialog.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class InputDialogDemo {
JLabel jlab;
JButton jbtnShow;
JFrame jfrm;
InputDialogDemo() {
// Create a new JFrame container.
jfrm = new JFrame("A Simple Input Dialog");
// Specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(400, 250);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a label that shows the response.
jlab = new JLabel();
// Create button that will display the dialog.
jbtnShow = new JButton("Show Dialog");
// Add action listener for the button.
jbtnShow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
// Create a dialog that inputs a string.
String response = JOptionPane.showInputDialog(
"Enter Name");
// If the response is null, then the dialog
// was cancelled or closed. If response is a
// zero-length string, then no input was entered.
// Otherwise, response contains a string entered
// by the user.
if(response == null)
jlab.setText("Dialog cancelled or closed");
else if(response.length() == 0)
jlab.setText("No string entered");
else
jlab.setText("Hi there " + response);
}
});
// Add the button and label to the content pane.
jfrm.add(jbtnShow);
jfrm.add(jlab);
// Display the frame.
jfrm.setVisible(true);
}
public static void main(String[] args) {
// Create the GUI on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new InputDialogDemo();
}
});
}
}
// -----------------------------------------
// Demonstrate an option dialog.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class OptionDialogDemo {
JLabel jlab;
JButton jbtnShow;
JFrame jfrm;
OptionDialogDemo() {
// Create a new JFrame container.
jfrm = new JFrame("A Simple Option Dialog");
// Specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(400, 250);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a label that will show the selection.
jlab = new JLabel();
// Create button that will display the dialog.
jbtnShow = new JButton("Show Dialog");
// Add action listener for the button.
jbtnShow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
// Define the connection options.
String[] connectOpts = { "Modem", "Wireless",
"Satellite", "Cable" };
// Create a dialog that lets the user
// choose how to connect to the network.
int response = JOptionPane.showOptionDialog(
jfrm,
"Choose Network Connection",
"Connection Type",
JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
connectOpts,
"Wireless");
// Display the choice.
switch(response) {
case 0:
jlab.setText("Connect via modem.");
break;
case 1:
jlab.setText("Connect via wireless.");
break;
case 2:
jlab.setText("Connect via satellite.");
break;
case 3:
jlab.setText("Connect via cable.");
break;
case JOptionPane.CLOSED_OPTION:
jlab.setText("Dialog cancelled.");
break;
}
}
});
// Add the button and label to the content pane.
jfrm.add(jbtnShow);
jfrm.add(jlab);
// Display the frame.
jfrm.setVisible(true);
}
public static void main(String[] args) {
// Create the GUI on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new OptionDialogDemo();
}
});
}
}
// -----------------------------------------
// Demonstrate a simple JDialog.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class JDialogDemo {
JLabel jlab;
JButton jbtnShow;
JButton jbtnReset;
// These buttons are contained within the dialog.
JButton jbtnUp;
JButton jbtnDown;
JDialog jdlg;
JDialogDemo() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("JDialog Demo");
// Specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(400, 200);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a label that shows the direction.
jlab = new JLabel("Direction is pending.");
// Create a button that will show the dialog.
jbtnShow = new JButton("Show Dialog");
// Create button that will reset the direction.
jbtnReset = new JButton("Reset Direction");
// Create a simple modal dialog.
jdlg = new JDialog(jfrm, "Direction", true);
jdlg.setSize(200, 100);
jdlg.setLayout(new FlowLayout());
// Create buttons used by the dialog.
jbtnUp = new JButton("Up");
jbtnDown = new JButton("Down");
// Add buttons to the dialog.
jdlg.add(jbtnUp);
jdlg.add(jbtnDown);
// Add a label to the dialog.
jdlg.add(new JLabel("Press a button."));
// Show the dialog when the Show Dialog button is pressed.
jbtnShow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jdlg.setVisible(true);
}
});
// Reset the direction when the Reset Direction
// button is pressed.
jbtnReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jlab.setText("Direction is pending.");
}
});
// Respond to the Up button in the dialog.
jbtnUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jlab.setText("Direction is Up");
// Hide the dialog after the user selects
// a direction.
jdlg.setVisible(false);
}
});
// Respond to the Down button in the dialog.
jbtnDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
jlab.setText("Direction is Down");
// Hide the dialog after the user selects
// a direction.
jdlg.setVisible(false);
}
});
// Add the Show Dialog button and label to the content pane.
jfrm.add(jbtnShow);
jfrm.add(jbtnReset);
jfrm.add(jlab);
// Display the frame.
jfrm.setVisible(true);
}
public static void main(String[] args) {
// Create the GUI on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JDialogDemo();
}
});
}
}
// -----------------------------------------
// Demonstrate JFileChooser.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FileChooserDemo {
JLabel jlab;
JButton jbtnShow;
JFileChooser jfc;
FileChooserDemo() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("JFileChooser Demo");
// Specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(400, 200);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a label that will show the selected file.
jlab = new JLabel();
// Create button that will show the dialog.
jbtnShow = new JButton("Show File Chooser");
// Create the file chooser.
jfc = new JFileChooser();
// Show the file chooser when the Show File Chooser button
// is pressed.
jbtnShow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
// Pass null for the parent. This centers the dialog
// on the screen.
int result = jfc.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION)
jlab.setText("Selected file is: " +
jfc.getSelectedFile().getName());
else
jlab.setText("No file selected.");
}
});
// Add the Show File Chooser button and label to the
// content pane.
jfrm.add(jbtnShow);
jfrm.add(jlab);
// Display the frame.
jfrm.setVisible(true);
}
public static void main(String[] args) {
// Create the GUI on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new FileChooserDemo();
}
});
}
}
// -----------------------------------------
// Try This 20-1: Demonstrate a custom file filter.
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
// A custom file filter that displays
// Java source files and directories.
class JavaFileFilter extends FileFilter {
public boolean accept(File file) {
// Return true if the file is a Java source file
// or if it is a directory.
if(file.getName().endsWith(".java")) return true;
if(file.isDirectory()) return true;
// Otherwise, return false.
return false;
}
public String getDescription() {
return "Java Source Code Files";
}
}
public class FileFilterDemo {
JLabel jlab;
JButton jbtnShow;
JFileChooser jfc;
FileFilterDemo() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("File Filter Demo");
// Specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(400, 200);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a label that will show the selected file.
jlab = new JLabel();
// Create a button that will show the dialog.
jbtnShow = new JButton("Show File Chooser");
// Create the file chooser.
jfc = new JFileChooser();
// Set the file filter.
jfc.setFileFilter(new JavaFileFilter());
// Show the file chooser when the Show File Chooser button
// is pressed.
jbtnShow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
// Pass null for the parent. This typically centers
// the dialog on the screen.
int result = jfc.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION)
jlab.setText("Selected file is: " +
jfc.getSelectedFile().getName());
else
jlab.setText("No file selected.");
}
});
// Add the Show File Chooser button and label to the
// content pane.
jfrm.add(jbtnShow);
jfrm.add(jlab);
// Display the frame.
jfrm.setVisible(true);
}
public static void main(String[] args) {
// Create the GUI on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new FileFilterDemo();
}
});
}
}