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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
|
<html>
<head>
<title>
Handbuch der Java-Programmierung, 5. Auflage
</title>
</head>
<body>
<a name="startofbody"></a>
<script language="JavaScript" src="hjp4lib.js">
</script>
<script language="JavaScript">
installKbdHandler("97,#startofbody;101,#endofbody;116,cover.html;122,k100003.html;115,search.html;105,index.html;100,JDKDOCS;112,APIDOCS;104,k100201.html;106,k100203.html;107,k100205.html;108,k100206.html");
</script>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100201.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100203.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100205.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100206.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">Kapitel 31 - GUI-Dialoge
</table>
<hr>
<!-- Section -->
<a name="sectlevel2id031003"></a>
<h2>31.3 <a name="ixa102150">Modale Dialoge</a><a name="ixa102151"></a></h2>
<hr>
<ul>
<li><a href="k100204.html#sectlevel2id031003">31.3 Modale Dialoge</a>
</ul>
<hr>
<p>
Modale Dialoge sind solche, die alle Benutzereingaben des Programms
beanspruchen und andere Fenster erst dann wieder zum Zuge kommen lassen,
wenn das Dialogfenster geschlossen wird. Eine wichtige Eigenschaft
modaler Dialoge ist es, dass im Programm der Aufruf zur Anzeige des
Dialogs so lange blockiert, bis der Dialog beendet ist. Auf diese
Weise kann an einer bestimmten Stelle im Programm auf eine Eingabe
gewartet werden und erst dann mit der Bearbeitung fortgefahren werden,
wenn die Eingabe erfolgt ist.
<p>
Im AWT des JDK 1.0 gab es einen schwerwiegenden Fehler, durch den
das Erzeugen <i>modaler</i> Dialoge unmöglich gemacht wurde.
Der Fehler hielt sich bis zur Version 1.0.2 und wurde erst mit Erscheinen
des JDK 1.1 behoben. Da viele Anwenderdialoge von Natur aus modalen
Charakter haben, wurde dieser Fehler von vielen als schwerwiegend
angesehen, und die Java-Gemeinde entwickelte eine Reihe von Workarounds,
die aber allesamt nicht voll zufriedenstellen konnten. Glücklicherweise
wurden die Probleme mit dem JDK 1.1 behoben, und wir wollen in diesem
Kapitel aufzeigen, wie modale Dialoge in Java erzeugt werden können.
<p>
Ein modaler Dialog muss immer aus der Klasse <a name="ixa102152"><a href="index_d.html#ixb101145"><font color=#000080><tt>Dialog</tt></font></a></a>
abgeleitet werden. Nur sie bietet die Möglichkeit, an den Konstruktor
einen booleschen Wert zu übergeben, der festlegt, dass die übrigen
Fenster der Anwendung während der Anzeige des Dialogs suspendiert
werden. <a href="index_d.html#ixb101145"><font color=#000080><tt>Dialog</tt></font></a>
besitzt folgende Konstruktoren:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public Dialog(Frame owner)
public Dialog(Frame owner, boolean modal)
public Dialog(Frame owner, String title)
public Dialog(Frame owner, String title, boolean modal)
public Dialog(Dialog owner)
public Dialog(Dialog owner, String title)
public Dialog(Dialog owner, String title, boolean modal)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/Dialog.html" onClick="this.href=getApiDoc('java.awt.Dialog')"><font color="#660066" size=-1>java.awt.Dialog</font></a></td>
</tr>
</table>
<p>
Als erstes Argument muss in jedem Fall ein <a href="index_f.html#ixb101144"><font color=#000080><tt>Frame</tt></font></a>-
oder <a href="index_d.html#ixb101145"><font color=#000080><tt>Dialog</tt></font></a>-Objekt
als Vaterfenster übergeben werden (bis zur Version 1.1 waren
nur <a href="index_f.html#ixb101144"><font color=#000080><tt>Frame</tt></font></a>-Objekte
erlaubt). Mit <font color="#000077"><tt>title</tt></font> kann der
Inhalt der Titelzeile vorgegeben werden, und der Parameter <font color="#000077"><tt>modal</tt></font>
entscheidet, ob der Dialog modal dargestellt wird oder nicht.
<p>
<a href="index_d.html#ixb101145"><font color=#000080><tt>Dialog</tt></font></a>
bietet die Methoden <a name="ixa102153"><a href="index_i.html#ixb101639"><font color=#000080><tt>isModal</tt></font></a></a>
und <a name="ixa102154"><a href="index_s.html#ixb101640"><font color=#000080><tt>setModal</tt></font></a></a>,
mit denen auf die Modalität des Dialogs zugegriffen werden kann:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public boolean isModal()
public void setModal(boolean b)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/Dialog.html" onClick="this.href=getApiDoc('java.awt.Dialog')"><font color="#660066" size=-1>java.awt.Dialog</font></a></td>
</tr>
</table>
<p>
Der Rückgabewert von <a href="index_i.html#ixb101639"><font color=#000080><tt>isModal</tt></font></a>
ist <a href="index_t.html#ixb100233"><font color=#000080><tt>true</tt></font></a>,
falls der Dialog modal ist. Andernfalls ist er <a href="index_f.html#ixb100234"><font color=#000080><tt>false</tt></font></a>.
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Die Methode <a href="index_s.html#ixb101640"><font color=#000080><tt>setModal</tt></font></a>,
deren Fähigkeit darin besteht, einen bestehenden Dialog zwischen
den Zuständen modal und nicht-modal umzuschalten, ist mit Vorsicht
zu genießen. Unter Windows 95 hat ein Aufruf im JDK 1.1 mitunter
dazu geführt, dass beim Schließen des Fensters die Nachrichtenschleife
des Aufrufers deaktiviert blieb und das Programm sich aufhängte.
Am besten, man entscheidet schon im Konstruktor, ob der Dialog modal
sein soll oder nicht.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#CC0000">
<tr>
<td><font color="#FFFFFF"> Warnung </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
</tr>
</table>
<p>
Ein zusätzliches Feature der Klasse <a href="index_d.html#ixb101145"><font color=#000080><tt>Dialog</tt></font></a>
besteht darin, die Veränderbarkeit der Größe eines
Fensters durch den Anwender zu unterbinden:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void setResizable(boolean resizable)
public boolean isResizable()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/awt/Dialog.html" onClick="this.href=getApiDoc('java.awt.Dialog')"><font color="#660066" size=-1>java.awt.Dialog</font></a></td>
</tr>
</table>
<p>
Nach einem Aufruf von <a name="ixa102155"><a href="index_s.html#ixb101641"><font color=#000080><tt>setResizable</tt></font></a></a>
mit <a href="index_f.html#ixb100234"><font color=#000080><tt>false</tt></font></a>
als Argument kann die Größe des Fensters nicht mehr vom
Anwender verändert werden. Nach einem Aufruf von <font color="#000077"><tt>setResizable(true)</tt></font>
ist dies wieder möglich. Mit <a name="ixa102156"><a href="index_i.html#ixb101642"><font color=#000080><tt>isResizable</tt></font></a></a>
kann der aktuelle Zustand dieses Schalters abgefragt werden.
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Auch die Methode <a href="index_s.html#ixb101641"><font color=#000080><tt>setResizable</tt></font></a>
ist nicht ganz unkritisch, denn sie hat auf einigen UNIX-Systemen
zu Problemen geführt. Sowohl unter SUN Solaris als auch unter
LINUX kam es im JDK 1.1 zu Hängern bei Programmen, die diese
Methode benutzten. Falls sich die nachfolgenden Beispielprogramme
auf Ihrem System nicht so verhalten wie angegeben (typischerweise
wird der Dialog gar nicht erst angezeigt), sollten Sie versuchsweise
den Aufruf von <a href="index_s.html#ixb101641"><font color=#000080><tt>setResizable</tt></font></a>
auskommentieren.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#CC0000">
<tr>
<td><font color="#FFFFFF"> Warnung </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
</tr>
</table>
<p>
Das folgende Beispiel demonstriert die Anwendung der Klasse <a href="index_d.html#ixb101145"><font color=#000080><tt>Dialog</tt></font></a>.
Es zeigt einen <a href="index_f.html#ixb101144"><font color=#000080><tt>Frame</tt></font></a>,
aus dem per Buttonklick ein modaler Dialog aufgerufen werden kann.
Der Dialog fragt den Anwender, ob die Anwendung beendet werden soll,
und wartet, bis einer der Buttons »Ja« oder »Nein«
gedrückt wurde. In diesem Fall wird ein boolescher Rückgabewert
generiert, der nach dem Schließen des Dialogs an das Vaterfenster
zurückgegeben wird. Falls der Rückgabewert <a href="index_t.html#ixb100233"><font color=#000080><tt>true</tt></font></a>
(entprechend dem Button »Ja«) ist, wird die Anwendung beendet,
andernfalls läuft sie weiter:
<a name="listingid031010"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Listing3110.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.awt.*;
<font color="#555555">004 </font><font color="#0000AA">import</font> java.awt.event.*;
<font color="#555555">005 </font>
<font color="#555555">006 </font><font color="#0000AA">class</font> YesNoDialog
<font color="#555555">007 </font><font color="#0000AA">extends</font> Dialog
<font color="#555555">008 </font><font color="#0000AA">implements</font> ActionListener
<font color="#555555">009 </font>{
<font color="#555555">010 </font> <font color="#006699">boolean</font> result;
<font color="#555555">011 </font>
<font color="#555555">012 </font> <font color="#0000AA">public</font> YesNoDialog(Frame owner, String msg)
<font color="#555555">013 </font> {
<font color="#555555">014 </font> <font color="#006699">super</font>(owner, <font color="#0000FF">"Ja-/Nein-Auswahl"</font>, <font color="#006699">true</font>);
<font color="#555555">015 </font> <font color="#00AA00">//Fenster</font>
<font color="#555555">016 </font> setBackground(Color.lightGray);
<font color="#555555">017 </font> setLayout(<font color="#0000AA">new</font> BorderLayout());
<font color="#555555">018 </font> setResizable(<font color="#006699">false</font>); <font color="#00AA00">//Hinweis im Text beachten</font>
<font color="#555555">019 </font> Point parloc = owner.getLocation();
<font color="#555555">020 </font> setLocation(parloc.x + 30, parloc.y + 30);
<font color="#555555">021 </font> <font color="#00AA00">//Message</font>
<font color="#555555">022 </font> add(<font color="#0000AA">new</font> Label(msg), BorderLayout.CENTER);
<font color="#555555">023 </font> <font color="#00AA00">//Buttons</font>
<font color="#555555">024 </font> Panel panel = <font color="#0000AA">new</font> Panel();
<font color="#555555">025 </font> panel.setLayout(<font color="#0000AA">new</font> FlowLayout(FlowLayout.CENTER));
<font color="#555555">026 </font> Button button = <font color="#0000AA">new</font> Button(<font color="#0000FF">"Ja"</font>);
<font color="#555555">027 </font> button.addActionListener(<font color="#006699">this</font>);
<font color="#555555">028 </font> panel.add(button);
<font color="#555555">029 </font> button = <font color="#0000AA">new</font> Button(<font color="#0000FF">"Nein"</font>);
<font color="#555555">030 </font> button.addActionListener(<font color="#006699">this</font>);
<font color="#555555">031 </font> panel.add(button);
<font color="#555555">032 </font> add(panel, BorderLayout.SOUTH);
<font color="#555555">033 </font> pack();
<font color="#555555">034 </font> }
<font color="#555555">035 </font>
<font color="#555555">036 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> actionPerformed(ActionEvent event)
<font color="#555555">037 </font> {
<font color="#555555">038 </font> result = event.getActionCommand().equals(<font color="#0000FF">"Ja"</font>);
<font color="#555555">039 </font> setVisible(<font color="#006699">false</font>);
<font color="#555555">040 </font> dispose();
<font color="#555555">041 </font> }
<font color="#555555">042 </font>
<font color="#555555">043 </font> <font color="#0000AA">public</font> <font color="#006699">boolean</font> getResult()
<font color="#555555">044 </font> {
<font color="#555555">045 </font> <font color="#0000AA">return</font> result;
<font color="#555555">046 </font> }
<font color="#555555">047 </font>}
<font color="#555555">048 </font>
<font color="#555555">049 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing3110
<font color="#555555">050 </font><font color="#0000AA">extends</font> Frame
<font color="#555555">051 </font><font color="#0000AA">implements</font> ActionListener
<font color="#555555">052 </font>{
<font color="#555555">053 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">054 </font> {
<font color="#555555">055 </font> Listing3110 wnd = <font color="#0000AA">new</font> Listing3110();
<font color="#555555">056 </font> wnd.setVisible(<font color="#006699">true</font>);
<font color="#555555">057 </font> }
<font color="#555555">058 </font>
<font color="#555555">059 </font> <font color="#0000AA">public</font> Listing3110()
<font color="#555555">060 </font> {
<font color="#555555">061 </font> <font color="#006699">super</font>(<font color="#0000FF">"Modale Dialoge"</font>);
<font color="#555555">062 </font> setLayout(<font color="#0000AA">new</font> FlowLayout());
<font color="#555555">063 </font> setBackground(Color.lightGray);
<font color="#555555">064 </font> Button button = <font color="#0000AA">new</font> Button(<font color="#0000FF">"Ende"</font>);
<font color="#555555">065 </font> button.addActionListener(<font color="#006699">this</font>);
<font color="#555555">066 </font> add(button);
<font color="#555555">067 </font> setLocation(100,100);
<font color="#555555">068 </font> setSize(300,200);
<font color="#555555">069 </font> setVisible(<font color="#006699">true</font>);
<font color="#555555">070 </font> }
<font color="#555555">071 </font>
<font color="#555555">072 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> actionPerformed(ActionEvent event)
<font color="#555555">073 </font> {
<font color="#555555">074 </font> String cmd = event.getActionCommand();
<font color="#555555">075 </font> <font color="#0000AA">if</font> (cmd.equals(<font color="#0000FF">"Ende"</font>)) {
<font color="#555555">076 </font> YesNoDialog dlg;
<font color="#555555">077 </font> dlg = <font color="#0000AA">new</font> YesNoDialog(
<font color="#555555">078 </font> <font color="#006699">this</font>,
<font color="#555555">079 </font> <font color="#0000FF">"Wollen Sie das Programm wirklich beenden?"</font>
<font color="#555555">080 </font> );
<font color="#555555">081 </font> dlg.setVisible(<font color="#006699">true</font>);
<font color="#555555">082 </font> <font color="#00AA00">//Auf das Schließen des Dialogs warten...</font>
<font color="#555555">083 </font> <font color="#0000AA">if</font> (dlg.getResult()) {
<font color="#555555">084 </font> setVisible(<font color="#006699">false</font>);
<font color="#555555">085 </font> dispose();
<font color="#555555">086 </font> System.exit(0);
<font color="#555555">087 </font> }
<font color="#555555">088 </font> }
<font color="#555555">089 </font> }
<font color="#555555">090 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing3110.java"><font color="#000055" size=-1>Listing3110.java</font></a></td>
</tr>
</table>
<i>
Listing 31.10: Konstruktion modaler Dialoge</i></p>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Um den Dialog relativ zur Position seines Vaterfensters anzuzeigen,
wird dessen Position durch Aufruf von <a name="ixa102157"><a href="index_g.html#ixb101358"><font color=#000080><tt>getLocation</tt></font></a></a>
ermittelt. Der Ursprung des Dialogfensters wird dann 30 Pixel weiter
nach unten bzw. nach rechts gelegt.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#000077">
<tr>
<td><font color="#FFFFFF"> Hinweis </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>
<p>
Nach dem Start zeigt das Programm zunächst das in <a href="k100204.html#modalvater">Abbildung 31.14</a>
dargestellte Fenster an:
<p>
<a name="modalvater"></a>
<img src="images/ModalDialog.gif">
<p>
<p><i>
Abbildung 31.14: Das Vaterfenster für den modalen Dialog</i></p>
<p>
Nach dem Klick auf »Ende« wird <a href="index_a.html#ixb101474"><font color=#000080><tt>actionPerformed</tt></font></a>
aufgerufen und läuft bis zum Aufruf des Dialogs (siehe <a href="k100204.html#janeindialog">Abbildung 31.15</a>).
Die Anweisung <font color="#000077"><tt>dlg.setVisible(true);</tt></font>
ruft den Dialog auf und blockiert dann. Sie terminiert erst, wenn
der Dialog durch Drücken eines Buttons beendet wurde. In diesem
Fall wird mit <font color="#000077"><tt>getResult</tt></font> der
Rückgabewert abgefragt und das Programm beendet, wenn dieser
<a href="index_t.html#ixb100233"><font color=#000080><tt>true</tt></font></a>
ist. Die Methode <font color="#000077"><tt>getResult</tt></font> liefert
den Inhalt der privaten Instanzvariablen <font color="#000077"><tt>result</tt></font>,
die beim Auftreten eines Action-Events durch einen der beiden Buttons
gesetzt wird. Zwar ist das Dialogfenster beim Aufruf von <font color="#000077"><tt>getResult</tt></font>
bereits zerstört, aber die Objektvariable <font color="#000077"><tt>dlg</tt></font>
existiert noch und <font color="#000077"><tt>getResult</tt></font>
kann aufgerufen werden.
<p>
<a name="janeindialog"></a>
<img src="images/JaNeinDialog.gif">
<p>
<p><i>
Abbildung 31.15: Ein einfacher Ja-/Nein-Dialog</i></p>
<p>
Bevor wir das Kapitel beenden, wollen wir dieses Programm ein wenig
erweitern und ein weiteres Beispiel für die Anwendung modaler
Dialoge geben. Das folgende Programm implementiert eine Klasse <font color="#000077"><tt>ModalDialog</tt></font>,
die aus <a href="index_d.html#ixb101145"><font color=#000080><tt>Dialog</tt></font></a>
abgeleitet ist. Mit ihrer Hilfe können modale Dialoge der obigen
Art konstruiert werden, bei denen die Ausstattung mit Buttons variabel
ist und zum Zeitpunkt der Instanzierung festgelegt werden kann:
<font color="#000077">
<pre>
public ModalDialog(
Frame owner,
String title,
String msg,
String buttons
);
</pre>
</font>
<p>
Der Konstruktor erwartet neben dem Vater-<a href="index_f.html#ixb101144"><font color=#000080><tt>Frame</tt></font></a>
drei weitere Parameter, <font color="#000077"><tt>title</tt></font>,
<font color="#000077"><tt>msg</tt></font> und <font color="#000077"><tt>buttons</tt></font>.
In <font color="#000077"><tt>title</tt></font> wird der Inhalt der
Titelzeile übergeben, und <font color="#000077"><tt>msg</tt></font>
spezifiziert den Text innerhalb des Dialogs. Der Parameter <font color="#000077"><tt>buttons</tt></font>
erwartet eine Liste von <a href="index_b.html#ixb101471"><font color=#000080><tt>Button</tt></font></a>-Bezeichnern,
die zur Festlegung der Anzahl und Beschriftung der Buttons dienen.
Die einzelnen Elemente sind durch Kommata zu trennen und werden innerhalb
des Dialogs mit einem <a name="ixa102158"><a href="index_s.html#ixb100541"><font color=#000080><tt>StringTokenizer</tt></font></a></a>
zerlegt. Der in <font color="#000077"><tt>getResult</tt></font> gelieferte
Rückgabewert des Dialogs ist - anders als im vorigen Beispiel
- ein <a href="index_s.html#ixb100117"><font color=#000080><tt>String</tt></font></a>
und entspricht der Beschriftung des zum Schließen verwendeten
Buttons.
<p>
<font color="#000077"><tt>ModalDialog</tt></font> enthält einige
statische Methoden, mit denen Dialoge mit einer festen Buttonstruktur
einfach erzeugt werden können. So erzeugt <font color="#000077"><tt>OKButton</tt></font>
lediglich einen Button mit der Beschriftung »OK«, <font color="#000077"><tt>YesNoDialog</tt></font>
zwei Buttons, »Ja« und »Nein«, und <font color="#000077"><tt>YesNoCancelDialog</tt></font>
erzeugt einen Dialog mit drei Buttons, »Ja«, »Nein«
und »Abbruch«. Das folgende Listing zeigt die Klasse <font color="#000077"><tt>ModalDialog</tt></font>
und ein Rahmenprogramm zum Testen:
<a name="listingid031011"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Listing3111.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.awt.*;
<font color="#555555">004 </font><font color="#0000AA">import</font> java.awt.event.*;
<font color="#555555">005 </font><font color="#0000AA">import</font> java.util.*;
<font color="#555555">006 </font>
<font color="#555555">007 </font><font color="#0000AA">class</font> ModalDialog
<font color="#555555">008 </font><font color="#0000AA">extends</font> Dialog
<font color="#555555">009 </font><font color="#0000AA">implements</font> ActionListener
<font color="#555555">010 </font>{
<font color="#555555">011 </font> String result;
<font color="#555555">012 </font>
<font color="#555555">013 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> String OKDialog(Frame owner, String msg)
<font color="#555555">014 </font> {
<font color="#555555">015 </font> ModalDialog dlg;
<font color="#555555">016 </font> dlg = <font color="#0000AA">new</font> ModalDialog(owner,<font color="#0000FF">"Nachricht"</font>,msg,<font color="#0000FF">"OK"</font>);
<font color="#555555">017 </font> dlg.setVisible(<font color="#006699">true</font>);
<font color="#555555">018 </font> <font color="#0000AA">return</font> dlg.getResult();
<font color="#555555">019 </font> }
<font color="#555555">020 </font>
<font color="#555555">021 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> String YesNoDialog(Frame owner, String msg)
<font color="#555555">022 </font> {
<font color="#555555">023 </font> ModalDialog dlg;
<font color="#555555">024 </font> dlg = <font color="#0000AA">new</font> ModalDialog(owner,<font color="#0000FF">"Frage"</font>,msg,<font color="#0000FF">"Ja,Nein"</font>);
<font color="#555555">025 </font> dlg.setVisible(<font color="#006699">true</font>);
<font color="#555555">026 </font> <font color="#0000AA">return</font> dlg.getResult();
<font color="#555555">027 </font> }
<font color="#555555">028 </font>
<font color="#555555">029 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> String YesNoCancelDialog(Frame owner,String msg)
<font color="#555555">030 </font> {
<font color="#555555">031 </font> ModalDialog dlg;
<font color="#555555">032 </font> dlg = <font color="#0000AA">new</font> ModalDialog(owner,<font color="#0000FF">"Frage"</font>,msg,<font color="#0000FF">"Ja,Nein,Abbruch"</font>);
<font color="#555555">033 </font> dlg.setVisible(<font color="#006699">true</font>);
<font color="#555555">034 </font> <font color="#0000AA">return</font> dlg.getResult();
<font color="#555555">035 </font> }
<font color="#555555">036 </font>
<font color="#555555">037 </font> <font color="#0000AA">public</font> ModalDialog(
<font color="#555555">038 </font> Frame owner,
<font color="#555555">039 </font> String title,
<font color="#555555">040 </font> String msg,
<font color="#555555">041 </font> String buttons
<font color="#555555">042 </font> )
<font color="#555555">043 </font> {
<font color="#555555">044 </font> <font color="#006699">super</font>(owner, title, <font color="#006699">true</font>);
<font color="#555555">045 </font> <font color="#00AA00">//Fenster</font>
<font color="#555555">046 </font> setBackground(Color.lightGray);
<font color="#555555">047 </font> setLayout(<font color="#0000AA">new</font> BorderLayout());
<font color="#555555">048 </font> setResizable(<font color="#006699">false</font>);
<font color="#555555">049 </font> Point parloc = owner.getLocation();
<font color="#555555">050 </font> setLocation(parloc.x + 30, parloc.y + 30);
<font color="#555555">051 </font> <font color="#00AA00">//Message</font>
<font color="#555555">052 </font> add(<font color="#0000AA">new</font> Label(msg), BorderLayout.CENTER);
<font color="#555555">053 </font> <font color="#00AA00">//Buttons</font>
<font color="#555555">054 </font> Panel panel = <font color="#0000AA">new</font> Panel();
<font color="#555555">055 </font> panel.setLayout(<font color="#0000AA">new</font> FlowLayout(FlowLayout.CENTER));
<font color="#555555">056 </font> StringTokenizer strtok = <font color="#0000AA">new</font> StringTokenizer(buttons,<font color="#0000FF">","</font>);
<font color="#555555">057 </font> <font color="#0000AA">while</font> (strtok.hasMoreTokens()) {
<font color="#555555">058 </font> Button button = <font color="#0000AA">new</font> Button(strtok.nextToken());
<font color="#555555">059 </font> button.addActionListener(<font color="#006699">this</font>);
<font color="#555555">060 </font> panel.add(button);
<font color="#555555">061 </font> }
<font color="#555555">062 </font> add(panel, BorderLayout.SOUTH);
<font color="#555555">063 </font> pack();
<font color="#555555">064 </font> }
<font color="#555555">065 </font>
<font color="#555555">066 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> actionPerformed(ActionEvent event)
<font color="#555555">067 </font> {
<font color="#555555">068 </font> result = event.getActionCommand();
<font color="#555555">069 </font> setVisible(<font color="#006699">false</font>);
<font color="#555555">070 </font> dispose();
<font color="#555555">071 </font> }
<font color="#555555">072 </font>
<font color="#555555">073 </font> <font color="#0000AA">public</font> String getResult()
<font color="#555555">074 </font> {
<font color="#555555">075 </font> <font color="#0000AA">return</font> result;
<font color="#555555">076 </font> }
<font color="#555555">077 </font>}
<font color="#555555">078 </font>
<font color="#555555">079 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing3111
<font color="#555555">080 </font><font color="#0000AA">extends</font> Frame
<font color="#555555">081 </font><font color="#0000AA">implements</font> ActionListener
<font color="#555555">082 </font>{
<font color="#555555">083 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">084 </font> {
<font color="#555555">085 </font> Listing3111 wnd = <font color="#0000AA">new</font> Listing3111();
<font color="#555555">086 </font> wnd.setVisible(<font color="#006699">true</font>);
<font color="#555555">087 </font> }
<font color="#555555">088 </font>
<font color="#555555">089 </font> <font color="#0000AA">public</font> Listing3111()
<font color="#555555">090 </font> {
<font color="#555555">091 </font> <font color="#006699">super</font>(<font color="#0000FF">"Drei modale Standarddialoge"</font>);
<font color="#555555">092 </font> setLayout(<font color="#0000AA">new</font> FlowLayout());
<font color="#555555">093 </font> setBackground(Color.lightGray);
<font color="#555555">094 </font> Button button = <font color="#0000AA">new</font> Button(<font color="#0000FF">"OKDialog"</font>);
<font color="#555555">095 </font> button.addActionListener(<font color="#006699">this</font>);
<font color="#555555">096 </font> add(button);
<font color="#555555">097 </font> button = <font color="#0000AA">new</font> Button(<font color="#0000FF">"YesNoDialog"</font>);
<font color="#555555">098 </font> button.addActionListener(<font color="#006699">this</font>);
<font color="#555555">099 </font> add(button);
<font color="#555555">100 </font> button = <font color="#0000AA">new</font> Button(<font color="#0000FF">"YesNoCancelDialog"</font>);
<font color="#555555">101 </font> button.addActionListener(<font color="#006699">this</font>);
<font color="#555555">102 </font> add(button);
<font color="#555555">103 </font> setLocation(100,100);
<font color="#555555">104 </font> setSize(400,200);
<font color="#555555">105 </font> setVisible(<font color="#006699">true</font>);
<font color="#555555">106 </font> }
<font color="#555555">107 </font>
<font color="#555555">108 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> actionPerformed(ActionEvent event)
<font color="#555555">109 </font> {
<font color="#555555">110 </font> String cmd = event.getActionCommand();
<font color="#555555">111 </font> <font color="#0000AA">if</font> (cmd.equals(<font color="#0000FF">"OKDialog"</font>)) {
<font color="#555555">112 </font> ModalDialog.OKDialog(<font color="#006699">this</font>,<font color="#0000FF">"Dream, dream, dream, ..."</font>);
<font color="#555555">113 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (cmd.equals(<font color="#0000FF">"YesNoDialog"</font>)) {
<font color="#555555">114 </font> String ret = ModalDialog.YesNoDialog(
<font color="#555555">115 </font> <font color="#006699">this</font>,
<font color="#555555">116 </font> <font color="#0000FF">"Programm beenden?"</font>
<font color="#555555">117 </font> );
<font color="#555555">118 </font> <font color="#0000AA">if</font> (ret.equals(<font color="#0000FF">"Ja"</font>)) {
<font color="#555555">119 </font> setVisible(<font color="#006699">false</font>);
<font color="#555555">120 </font> dispose();
<font color="#555555">121 </font> System.exit(0);
<font color="#555555">122 </font> }
<font color="#555555">123 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (cmd.equals(<font color="#0000FF">"YesNoCancelDialog"</font>)) {
<font color="#555555">124 </font> String msg = <font color="#0000FF">"Verzeichnis erstellen?"</font>;
<font color="#555555">125 </font> String ret = ModalDialog.YesNoCancelDialog(<font color="#006699">this</font>,msg);
<font color="#555555">126 </font> ModalDialog.OKDialog(<font color="#006699">this</font>,<font color="#0000FF">"Rückgabe: "</font> + ret);
<font color="#555555">127 </font> }
<font color="#555555">128 </font> }
<font color="#555555">129 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing3111.java"><font color="#000055" size=-1>Listing3111.java</font></a></td>
</tr>
</table>
<i>
Listing 31.11: Drei modale Standarddialoge</i></p>
<p>
Die drei Dialoge, die durch Aufrufe von <font color="#000077"><tt>OKDialog</tt></font>,
<font color="#000077"><tt>YesNoDialog</tt></font> und <font color="#000077"><tt>YesNoCancelDialog</tt></font>
generiert werden, sind in den folgenden Abbildungen <a href="k100204.html#aufrufokdialog">31.16</a>
bis <a href="k100204.html#aufrufyesnocanceldialog">31.18</a> dargestellt:
<p>
<a name="aufrufokdialog"></a>
<img src="images/OKDialog.gif">
<p>
<p><i>
Abbildung 31.16: Ein Aufruf von OKDialog</i></p>
<p>
<a name="imageid031017"></a>
<img src="images/YesNoDialog.gif">
<p>
<p><i>
Abbildung 31.17: Ein Aufruf von YesNoDialog</i></p>
<p>
<a name="aufrufyesnocanceldialog"></a>
<img src="images/YesNoCancelDialog.gif">
<p>
<p><i>
Abbildung 31.18: Ein Aufruf von YesNoCancelDialog</i></p>
<hr>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage, Addison
Wesley, Version 5.0.1
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100201.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100203.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100205.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100206.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">© 1998, 2007 Guido Krüger & Thomas
Stark, <a href="http://www.javabuch.de">http://www.javabuch.de</a>
</table>
<a name="endofbody"></a>
</body>
</html>
|