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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
|
<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,k100279.html;106,k100284.html;107,k100286.html;108,k100287.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="k100279.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100284.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100286.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100287.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 44 - Beans
</table>
<hr>
<!-- Section -->
<a name="beancustomization"></a>
<h2>44.6 BeanInfo-Klassen und Property-Editoren </h2>
<hr>
<ul>
<li><a href="k100285.html#beancustomization">44.6 BeanInfo-Klassen und Property-Editoren</a>
<ul>
<li><a href="k100285.html#sectlevel3id044006001">44.6.1 BeanInfo-Klassen</a>
<ul>
<li><a href="k100285.html#sectlevel4id044006001001">getIcon</a>
<li><a href="k100285.html#sectlevel4id044006001002">getPropertyDescriptors</a>
<li><a href="k100285.html#sectlevel4id044006001003">getMethodDescriptors</a>
<li><a href="k100285.html#sectlevel4id044006001004">Die Klasse LightBulbBeanInfo</a>
</ul>
<li><a href="k100285.html#propertyeditoren">44.6.2 Property-Editoren</a>
<ul>
<li><a href="k100285.html#sectlevel4id044006002001">LightBulbLightOnEditor1</a>
<li><a href="k100285.html#sectlevel4id044006002002">LightBulbLightOnEditor2</a>
<li><a href="k100285.html#sectlevel4id044006002003">LightBulbLightOnEditor3</a>
</ul>
</ul>
</ul>
<hr>
<!-- Section -->
<a name="sectlevel3id044006001"></a>
<h3>44.6.1 BeanInfo-Klassen </h3>
<p>
Die bisher beschriebenen Fähigkeiten und Eigenschaften einer
Bean wurden vom GUI-Designer automatisch erkannt, weil die Beans bestimmte
Design-Konventionen eingehalten haben. Der GUI-Designer verwendet
eine Instanz der Klasse <a name="ixa103302"><a href="index_i.html#ixb102443"><font color=#000080><tt>Introspector</tt></font></a></a>
aus dem Paket <a href="index_j.html#ixb100594"><font color=#000080><tt>java.beans</tt></font></a>,
um diese Fähigkeiten nach dem Laden der Bean zu bestimmen.
<p>
Neben getter-/setter-Methoden und Registrierungsmethoden für
Events können Beans aber noch weitere Eigenschaften besitzen.
So kann beispielsweise in der Toolbox ein Icon angezeigt werden. Auch
Methoden, die nicht den getter-/setter-Konventionen entsprechen, können
bekanntgemacht werden. Zudem ist es möglich, für Nicht-Standard-Eigenschaften
spezielle Eigenschafteneditoren zur Verfügung zu stellen.
<p>
Diese Möglichkeiten stehen erst zur Verfügung, wenn man
die Bean mit einer expliziten <a name="ixa103303"><i>BeanInfo-Klasse</i></a>
ausstattet. Sie hat denselben Namen wie die Bean, trägt aber
zusätzlich den Suffix »BeanInfo« am Ende ihres Namens.
Zudem muss sie im selben Paket liegen wie die Bean-Klasse. Die BeanInfo-Klasse
muss das Interface <a name="ixa103304"><a href="index_b.html#ixb102422"><font color=#000080><tt>BeanInfo</tt></font></a></a>
implementieren oder aus <a name="ixa103305"><a href="index_s.html#ixb102445"><font color=#000080><tt>SimpleBeanInfo</tt></font></a></a>
abgeleitet sein (letzteres bietet den Vorteil, dass bereits alle erforderlichen
Methoden vorhanden sind und bei Bedarf nur noch überlagert werden
müssen).
<p>
<a href="index_b.html#ixb102422"><font color=#000080><tt>BeanInfo</tt></font></a>
definiert eine Reihe von Methoden, die der GUI-Designer abfragt. Sie
liefern jeweils ein Array von Descriptor-Objekten, von denen jedes
Informationen über eine zu veröffentlichende Methode, Eigenschaft
oder ein Icon zur Verfügung stellt. Wir wollen uns die drei wichigsten
von ihnen in den nachfolgenden Abschnitten ansehen.
<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>
Während der GUI-Designer die ihm bekannten Beans lädt, sucht
er für jede von ihnen zunächst nach einer passenden BeanInfo-Klasse.
Findet er eine solche, wird sie verwendet, und der Low-Level-Introspectionsvorgang
wird gar nicht erst angestoßen. Daraus folgt insbesondere, dass
in diesem Fall <i>nur die in der BeanInfo-Klasse angegebenen</i> Merkmale
zur Verfügung stehen.
<p>
Ist es beispielsweise nötig, eine BeanInfo-Klasse zur Verfügung
zu stellen, weil dem Designer eine besondere Methode bekanntgemacht
werden soll, die nicht den getter-/setter-Konventionen entspricht,
muss die BeanInfo-Klasse auch alle anderen Eigenschaften der Bean
explizit bekanntmachen. Andernfalls würde der GUI-Designer nur
die spezielle Methode sehen, alle anderen Merkmale aber ignorieren.</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>
<!-- Section -->
<a name="sectlevel4id044006001001"></a>
<h4>getIcon </h4>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public Image getIcon(int iconKind)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/beans/BeanInfo.html" onClick="this.href=getApiDoc('java.beans.BeanInfo')"><font color="#660066" size=-1>java.beans.BeanInfo</font></a></td>
</tr>
</table>
<p>
Der GUI-Designer ruft <a name="ixa103306"><a href="index_g.html#ixb102446"><font color=#000080><tt>getIcon</tt></font></a></a>
auf, um herauszufinden, ob die Bean ein Icon zur Darstellung in der
Toolbox zur Verfügung stellen kann. Als Parameter wird dabei
eine der symbolischen Konstanten <a name="ixa103307"><a href="index_i.html#ixb102447"><font color=#000080><tt>ICON_COLOR_16x16</tt></font></a></a>,
<a name="ixa103308"><a href="index_i.html#ixb102448"><font color=#000080><tt>ICON_MONO_16x16</tt></font></a></a>,
<a name="ixa103309"><a href="index_i.html#ixb102449"><font color=#000080><tt>ICON_COLOR_32x32</tt></font></a></a>
oder <a name="ixa103310"><a href="index_i.html#ixb102450"><font color=#000080><tt>ICON_MONO_32x32</tt></font></a></a>
übergeben. Kann die BeanInfo-Klasse ein Icon in der gewünschten
Form zur Verfügung stellen, muss sie ein passendes <a href="index_i.html#ixb100643"><font color=#000080><tt>Image</tt></font></a>-Objekt
erzeugen und an den Aufrufer zurückgeben. Ist das nicht der Fall,
sollte <a href="index_n.html#ixb100235"><font color=#000080><tt>null</tt></font></a>
zurückgegeben werden.
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#0099CC"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#0099CC"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Ist die BeanInfo-Klasse aus <a href="index_s.html#ixb102445"><font color=#000080><tt>SimpleBeanInfo</tt></font></a>
abgeleitet, ist das Erzeugen eines <a href="index_i.html#ixb100643"><font color=#000080><tt>Image</tt></font></a>-Objekts
einfach. <a href="index_s.html#ixb102445"><font color=#000080><tt>SimpleBeanInfo</tt></font></a>
besitzt eine Methode <a name="ixa103311"><a href="index_l.html#ixb102451"><font color=#000080><tt>loadImage</tt></font></a></a>,
die bei Übergabe eines Dateinamens die ensprechende Bilddatei
lädt und daraus ein <a href="index_i.html#ixb100643"><font color=#000080><tt>Image</tt></font></a>
erzeugt.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#0099CC">
<tr>
<td><font color="#FFFFFF"> Tipp </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#0099CC"><img src="trp1_1.gif"></td>
</tr>
</table>
<!-- Section -->
<a name="sectlevel4id044006001002"></a>
<h4>getPropertyDescriptors </h4>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public PropertyDescriptor[] getPropertyDescriptors()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/beans/BeanInfo.html" onClick="this.href=getApiDoc('java.beans.BeanInfo')"><font color="#660066" size=-1>java.beans.BeanInfo</font></a></td>
</tr>
</table>
<p>
Die Methode <a name="ixa103312"><a href="index_g.html#ixb102452"><font color=#000080><tt>getPropertyDescriptors</tt></font></a></a>
wird aufgerufen, um ein Array von <a name="ixa103313"><a href="index_p.html#ixb102453"><font color=#000080><tt>PropertyDescriptor</tt></font></a></a>-Objekten
zur Verfügung zu stellen. Es enthält für jede öffentliche
Eigenschaft der Bean ein Element, das dessen Merkmale beschreibt.
Ein <a href="index_p.html#ixb102453"><font color=#000080><tt>PropertyDescriptor</tt></font></a>
kann recht einfach instanziert werden:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public PropertyDescriptor(String propertyName, Class beanClass)
throws IntrospectionException
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/beans/PropertyDescriptor.html" onClick="this.href=getApiDoc('java.beans.PropertyDescriptor')"><font color="#660066" size=-1>java.beans.PropertyDescriptor</font></a></td>
</tr>
</table>
<p>
Lediglich der Name der Eigenschaft und das Klassenobjekt der zugehörigen
Bean sind anzugeben. Weitere Merkmale können durch Methodenaufrufe
hinzugefügt werden. Uns interessiert an dieser Stelle lediglich
die Methode <a name="ixa103314"><a href="index_s.html#ixb102454"><font color=#000080><tt>setPropertyEditorClass</tt></font></a></a>,
auf die wir in <a href="k100285.html#propertyeditoren">Abschnitt 44.6.2</a>
zurückkommen werden.
<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>
Für jedes von <a href="index_g.html#ixb102452"><font color=#000080><tt>getPropertyDescriptors</tt></font></a>
zurückgegebene Element erwartet der GUI-Designer eine setter-
und eine getter-Methode, deren Signatur dem bekannten Schema entsprechen
muss.</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>
<!-- Section -->
<a name="sectlevel4id044006001003"></a>
<h4>getMethodDescriptors </h4>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public MethodDescriptor[] getMethodDescriptors()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/beans/BeanInfo.html" onClick="this.href=getApiDoc('java.beans.BeanInfo')"><font color="#660066" size=-1>java.beans.BeanInfo</font></a></td>
</tr>
</table>
<p>
Zusätzliche Methoden können dem GUI-Designer durch Überlagern
von <a name="ixa103315"><a href="index_g.html#ixb102455"><font color=#000080><tt>getMethodDescriptors</tt></font></a></a>
bekanntgemacht werden. Zurückgegeben wird ein Array, das für
jede Methode ein Objekt des Typs <a name="ixa103316"><a href="index_m.html#ixb102456"><font color=#000080><tt>MethodDescriptor</tt></font></a></a>
enthält. Darin wird im wesentlichen ein <a name="ixa103317"><a href="index_m.html#ixb102337"><font color=#000080><tt>Method</tt></font></a></a>-Objekt
gekapselt (siehe <a href="k100274.html#reflectionmethodenaufrufe">Abschnitt 43.3</a>):
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public MethodDescriptor(Method method)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/beans/MethodDescriptor.html" onClick="this.href=getApiDoc('java.beans.MethodDescriptor')"><font color="#660066" size=-1>java.beans.MethodDescriptor</font></a></td>
</tr>
</table>
<!-- Section -->
<a name="sectlevel4id044006001004"></a>
<h4>Die Klasse LightBulbBeanInfo </h4>
<p>
Nach diesen Vorbemerkungen können wir eine geeignete BeanInfo-Klasse
für die <font color="#000077"><tt>LightBulb</tt></font>-Komponente
entwickeln:
<a name="lightbulbbeaninfo"></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">/* LightBulbBeanInfo.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.beans.*;
<font color="#555555">005 </font><font color="#0000AA">import</font> java.lang.reflect.*;
<font color="#555555">006 </font>
<font color="#555555">007 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> LightBulbBeanInfo
<font color="#555555">008 </font><font color="#0000AA">extends</font> SimpleBeanInfo
<font color="#555555">009 </font>{
<font color="#555555">010 </font> <font color="#0000AA">public</font> Image getIcon(<font color="#006699">int</font> iconKind)
<font color="#555555">011 </font> {
<font color="#555555">012 </font> String imgname = <font color="#0000FF">"bulbico16.gif"</font>;
<font color="#555555">013 </font> <font color="#0000AA">if</font> (iconKind == BeanInfo.ICON_MONO_32x32 ||
<font color="#555555">014 </font> iconKind == BeanInfo.ICON_COLOR_32x32) {
<font color="#555555">015 </font> imgname = <font color="#0000FF">"bulbico32.gif"</font>;
<font color="#555555">016 </font> }
<font color="#555555">017 </font> <font color="#0000AA">return</font> loadImage(imgname);
<font color="#555555">018 </font> }
<font color="#555555">019 </font>
<font color="#555555">020 </font> <font color="#0000AA">public</font> PropertyDescriptor[] getPropertyDescriptors()
<font color="#555555">021 </font> {
<font color="#555555">022 </font> <font color="#0000AA">try</font> {
<font color="#555555">023 </font> PropertyDescriptor pd1 = <font color="#0000AA">new</font> PropertyDescriptor(
<font color="#555555">024 </font> <font color="#0000FF">"lightOn"</font>,
<font color="#555555">025 </font> LightBulb.<font color="#0000AA">class</font>
<font color="#555555">026 </font> );
<font color="#555555">027 </font> <font color="#00AA00">//pd1.setPropertyEditorClass(LightBulbLightOnEditor1.class); <a name="lightbulbbeaninfo.a"></a></font>
<font color="#555555">028 </font> PropertyDescriptor[] ret = {pd1};
<font color="#555555">029 </font> <font color="#0000AA">return</font> ret;
<font color="#555555">030 </font> } <font color="#0000AA">catch</font> (IntrospectionException e) {
<font color="#555555">031 </font> System.err.println(e.toString());
<font color="#555555">032 </font> <font color="#0000AA">return</font> <font color="#006699">null</font>;
<font color="#555555">033 </font> }
<font color="#555555">034 </font> }
<font color="#555555">035 </font>
<font color="#555555">036 </font> <font color="#0000AA">public</font> MethodDescriptor[] getMethodDescriptors()
<font color="#555555">037 </font> {
<font color="#555555">038 </font> MethodDescriptor[] ret = <font color="#006699">null</font>;
<font color="#555555">039 </font> <font color="#0000AA">try</font> {
<font color="#555555">040 </font> Class bulbclass = LightBulb.<font color="#0000AA">class</font>;
<font color="#555555">041 </font> Method meth1 = bulbclass.getMethod(<font color="#0000FF">"toggleLight"</font>, <font color="#006699">null</font>);
<font color="#555555">042 </font> ret = <font color="#0000AA">new</font> MethodDescriptor[1];
<font color="#555555">043 </font> ret[0] = <font color="#0000AA">new</font> MethodDescriptor(meth1);
<font color="#555555">044 </font> } <font color="#0000AA">catch</font> (NoSuchMethodException e) {
<font color="#555555">045 </font> <font color="#00AA00">//ret bleibt null</font>
<font color="#555555">046 </font> }
<font color="#555555">047 </font> <font color="#0000AA">return</font> ret;
<font color="#555555">048 </font> }
<font color="#555555">049 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/LightBulbBeanInfo.java"><font color="#000055" size=-1>LightBulbBeanInfo.java</font></a></td>
</tr>
</table>
<i>
Listing 44.10: Die Klasse LightBulbBeanInfo</i></p>
<p>
Wird die Klasse übersetzt und in die jar-Datei für die Beanbox
aufgenommen, verändert sich die Präsentation der <font color="#000077"><tt>LighBulb</tt></font>-Bean
in der Beanbox:
<ul>
<li>In der Toolbox wird neben dem Namen das 16x16-Icon angezeigt.
<li>Der Eigenschafteneditor kennt nur noch die Eigenschaft »lightOn«.
<li>Der GUI-Designer hat registriert, dass es in der Methodenliste
von <font color="#000077"><tt>LightBulb</tt></font> zusätzlich
eine parameterlose Methode <font color="#000077"><tt>toggleLigth</tt></font>
gibt.
</ul>
<p>
Während die ersten beiden Veränderungen nach dem Starten
der Beanbox offensichtlich sind, können wir die letzte überprüfen,
indem wir den Aufruf von <font color="#000077"><tt>toggleLight</tt></font>
auf das Ereignis einer anderen Bean legen. Dazu platzieren wir im
GUI-Designer zunächst eine <font color="#000077"><tt>LightBulb</tt></font>-
und eine <font color="#000077"><tt>LightedPushButton</tt></font>-Bean.
Anschließend markieren wir die <font color="#000077"><tt>LightedPushButton</tt></font>-Bean
und rufen den Menüpunkt »Edit.Events.PropertyChange.propertyChange«
auf. Die rote Verbindungslinie ziehen wir auf die <font color="#000077"><tt>LightBulb</tt></font>
und fixieren sie dort mit einem Mausklick.
<p>
Aus der Auswahlliste der verfügbaren parameterlosen Methoden
können wir nun <font color="#000077"><tt>toggleLight</tt></font>
auswählen und »OK« drücken. Die Beanbox generiert
und übersetzt nun eine Adapterklasse, die bei jedem <a href="index_p.html#ixb102426"><font color=#000080><tt>PropertyChangeEvent</tt></font></a>
des <font color="#000077"><tt>LightedPushButton</tt></font> die Methode
<font color="#000077"><tt>toggleLight</tt></font> der <font color="#000077"><tt>LightBulb</tt></font>
aufruft. Nach jedem Drücken des Buttons verändert die Lampe
also ihren Zustand. Die Lampe kann natürlich nach wie vor auch
im Eigenschaftenfenster an- und ausgeschaltet werden.
<!-- Section -->
<a name="propertyeditoren"></a>
<h3>44.6.2 Property-Editoren </h3>
<p>
Die Beanbox und andere GUI-Designer stellen für einfache Eigenschaften
vordefinierte Editoren zur Verfügung, mit denen ihr Wert verändert
werden kann. Bereits bei indizierten Eigenschaften muss die Beanbox
aber passen. Auch für Objekttypen kann ein GUI-Designer keinen
Standard-Editor zur Verfügung stellen, weil er die Konfigurationsschnittstelle
des Objekts nicht kennt. Für diesen Zweck bietet die Beans-Architektur
die Möglichkeit, eigene Editoren zu definieren und bestimmten
Eigenschaften von Beans zuzuordnen.
<p>
In <a href="k100285.html#lightbulbbeaninfo.a">Zeile 027</a> von <a href="k100285.html#lightbulbbeaninfo">Listing 44.10</a>
ist der Aufruf von <font color="#000077"><tt>setPropertyEditorClass</tt></font>
auskommentiert, damit die Beanbox den eingebauten Editor für
boolesche Werte verwendet. Durch Entfernen des Kommentars erhält
der <a href="index_p.html#ixb102453"><font color=#000080><tt>PropertyDescriptor</tt></font></a>
der Eigenschaft »lightOn« die Information, dass zum Editieren
dieser Eigenschaft ein benutzerdefinierter Editor verwendet werden
soll.
<p>
Benutzerdefinierte Eigenschafteneditoren werden üblicherweise
aus der Klasse <a name="ixa103318"><a href="index_p.html#ixb102457"><font color=#000080><tt>PropertyEditorSupport</tt></font></a></a>
des Pakets <a href="index_j.html#ixb100594"><font color=#000080><tt>java.beans</tt></font></a>
abgeleitet. Sie besitzt eine Reihe von Methoden, die in eigenen Editoren
überlagert werden:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void setValue(Object value)
public Object getValue()
public String getAsText()
public void setAsText(String text)
throws java.lang.IllegalArgumentException
public String[] getTags()
public boolean isPaintable()
public boolean supportsCustomEditor()
public void paintValue(Graphics g, Rectangle box)
public Component getCustomEditor()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/beans/PropertyEditorSupport.html" onClick="this.href=getApiDoc('java.beans.PropertyEditorSupport')"><font color="#660066" size=-1>java.beans.PropertyEditorSupport</font></a></td>
</tr>
</table>
<p>
In den nächsten Abschnitten werden wir drei Editoren für
die Eigenschaft »lightOn« vorstellen. Sie machen in unterschiedlicher
Weise von diesen Methoden Gebrauch.
<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>
Eine weitere Variante zur Konfiguration von Beans (auf die wir hier
nicht näher eingehen wollen) ist die Implementierung eines <a name="ixa103319"><i>Customizers</i></a>.
Dieser erhält volle Kontrolle über die Konfiguration der
Bean. Er kann insbesondere auch mehrere Eigenschaften gleichzeitig
verändern und auf diese Weise aufwändige Editoren zur Verfügung
stellen, die den bekannten <i>Wizards</i> oder <i>Assistenten</i>
moderner Entwicklungssysteme ähneln.</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>
<!-- Section -->
<a name="sectlevel4id044006002001"></a>
<h4>LightBulbLightOnEditor1 </h4>
<p>
Der erste Editor ist sehr einfach aufgebaut. Er stellt ein Textfeld
zur Verfügung, in dem die Werte »an« und »aus«
eingegeben werden können, um den Zustand der Lampe umzuschalten.
<p>
Jeder der nachfolgend vorgestellten Editoren kapselt eine boolesche
Membervariable <font color="#000077"><tt>currentvalue</tt></font>,
die den aktuellen Zustand der Lampe festhält. Sie wird vom GUI-Designer
durch Aufruf von <a name="ixa103320"><a href="index_g.html#ixb100752"><font color=#000080><tt>getValue</tt></font></a></a>
abgefragt und durch <a name="ixa103321"><a href="index_s.html#ixb101686"><font color=#000080><tt>setValue</tt></font></a></a>
gesetzt. Beide Methoden operieren mit Objekttypen, d.h. es ist jeweils
eine Konvertierung zwischen <a href="index_b.html#ixb100072"><font color=#000080><tt>boolean</tt></font></a>
und <a href="index_b.html#ixb100468"><font color=#000080><tt>Boolean</tt></font></a>
erforderlich. In unserem Beispiel werden zusätzlich die Methoden
<a name="ixa103322"><a href="index_g.html#ixb102459"><font color=#000080><tt>getAsText</tt></font></a></a>
und <a name="ixa103323"><a href="index_s.html#ixb102460"><font color=#000080><tt>setAsText</tt></font></a></a>
überlagert, um auf die textuelle Repräsentation (»an«
und »aus«) des booleschen Werts zuzugreifen.
<a name="listingid044011"></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">/* LightBulbLightOnEditor1.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.beans.*;
<font color="#555555">005 </font>
<font color="#555555">006 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> LightBulbLightOnEditor1
<font color="#555555">007 </font><font color="#0000AA">extends</font> PropertyEditorSupport
<font color="#555555">008 </font>{
<font color="#555555">009 </font> <font color="#006699">boolean</font> currentvalue;
<font color="#555555">010 </font>
<font color="#555555">011 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> setValue(Object value)
<font color="#555555">012 </font> {
<font color="#555555">013 </font> currentvalue = ((Boolean)value).booleanValue();
<font color="#555555">014 </font> }
<font color="#555555">015 </font>
<font color="#555555">016 </font> <font color="#0000AA">public</font> Object getValue()
<font color="#555555">017 </font> {
<font color="#555555">018 </font> <font color="#0000AA">return</font> <font color="#0000AA">new</font> Boolean(currentvalue);
<font color="#555555">019 </font> }
<font color="#555555">020 </font>
<font color="#555555">021 </font> <font color="#0000AA">public</font> String getAsText()
<font color="#555555">022 </font> {
<font color="#555555">023 </font> <font color="#0000AA">return</font> <font color="#0000FF">""</font> + (currentvalue ? <font color="#0000FF">"an"</font> : <font color="#0000FF">"aus"</font>);
<font color="#555555">024 </font> }
<font color="#555555">025 </font>
<font color="#555555">026 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> setAsText(String text)
<font color="#555555">027 </font> <font color="#0000AA">throws</font> java.lang.IllegalArgumentException
<font color="#555555">028 </font> {
<font color="#555555">029 </font> <font color="#0000AA">if</font> (text.equalsIgnoreCase(<font color="#0000FF">"an"</font>)) {
<font color="#555555">030 </font> currentvalue = <font color="#006699">true</font>;
<font color="#555555">031 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (text.equalsIgnoreCase(<font color="#0000FF">"aus"</font>)) {
<font color="#555555">032 </font> currentvalue = <font color="#006699">false</font>;
<font color="#555555">033 </font> } <font color="#0000AA">else</font> {
<font color="#555555">034 </font> <font color="#0000AA">throw</font> <font color="#0000AA">new</font> IllegalArgumentException(text);
<font color="#555555">035 </font> }
<font color="#555555">036 </font> firePropertyChange();
<font color="#555555">037 </font> }
<font color="#555555">038 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/LightBulbLightOnEditor1.java"><font color="#000055" size=-1>LightBulbLightOnEditor1.java</font></a></td>
</tr>
</table>
<i>
Listing 44.11: Die Klasse LightBulbLightOnEditor1</i></p>
<p>
Der erste Editor sieht in der Beanbox so aus:
<p>
<a name="imageid044008"></a>
<img src="images/BulbEditor1.gif">
<p>
<p><i>
Abbildung 44.8: LightBulbLightOnEditor1 in der Beanbox</i></p>
<!-- Section -->
<a name="sectlevel4id044006002002"></a>
<h4>LightBulbLightOnEditor2 </h4>
<p>
Ist der Wertevorrat für die zu editierende Eigenschaft endlich,
kann es bequemer sein, dem Anwender alle möglichen Varianten
in einer Combox anzubieten. Dazu muss lediglich die Methode <a name="ixa103324"><a href="index_g.html#ixb102461"><font color=#000080><tt>getTags</tt></font></a></a>
überlagert und ein Array von Strings mit den möglichen Werten
zurückgegeben werden:
<a name="listingid044012"></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">/* LightBulbLightOnEditor2.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.beans.*;
<font color="#555555">005 </font>
<font color="#555555">006 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> LightBulbLightOnEditor2
<font color="#555555">007 </font><font color="#0000AA">extends</font> PropertyEditorSupport
<font color="#555555">008 </font>{
<font color="#555555">009 </font> <font color="#006699">boolean</font> currentvalue;
<font color="#555555">010 </font>
<font color="#555555">011 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> setValue(Object value)
<font color="#555555">012 </font> {
<font color="#555555">013 </font> currentvalue = ((Boolean)value).booleanValue();
<font color="#555555">014 </font> }
<font color="#555555">015 </font>
<font color="#555555">016 </font> <font color="#0000AA">public</font> Object getValue()
<font color="#555555">017 </font> {
<font color="#555555">018 </font> <font color="#0000AA">return</font> <font color="#0000AA">new</font> Boolean(currentvalue);
<font color="#555555">019 </font> }
<font color="#555555">020 </font>
<font color="#555555">021 </font> <font color="#0000AA">public</font> String getAsText()
<font color="#555555">022 </font> {
<font color="#555555">023 </font> <font color="#0000AA">return</font> <font color="#0000FF">""</font> + (currentvalue ? <font color="#0000FF">"an"</font> : <font color="#0000FF">"aus"</font>);
<font color="#555555">024 </font> }
<font color="#555555">025 </font>
<font color="#555555">026 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> setAsText(String text)
<font color="#555555">027 </font> <font color="#0000AA">throws</font> java.lang.IllegalArgumentException
<font color="#555555">028 </font> {
<font color="#555555">029 </font> System.out.println(<font color="#0000FF">"setAsText("</font> + text + <font color="#0000FF">")"</font>);
<font color="#555555">030 </font> <font color="#0000AA">if</font> (text.equalsIgnoreCase(<font color="#0000FF">"an"</font>)) {
<font color="#555555">031 </font> currentvalue = <font color="#006699">true</font>;
<font color="#555555">032 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (text.equalsIgnoreCase(<font color="#0000FF">"aus"</font>)) {
<font color="#555555">033 </font> currentvalue = <font color="#006699">false</font>;
<font color="#555555">034 </font> } <font color="#0000AA">else</font> {
<font color="#555555">035 </font> <font color="#0000AA">throw</font> <font color="#0000AA">new</font> IllegalArgumentException(text);
<font color="#555555">036 </font> }
<font color="#555555">037 </font> firePropertyChange();
<font color="#555555">038 </font> }
<font color="#555555">039 </font>
<font color="#555555">040 </font> <font color="#0000AA">public</font> String[] getTags()
<font color="#555555">041 </font> {
<font color="#555555">042 </font> <font color="#0000AA">return</font> <font color="#0000AA">new</font> String[]{<font color="#0000FF">"aus"</font>, <font color="#0000FF">"an"</font>};
<font color="#555555">043 </font> }
<font color="#555555">044 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/LightBulbLightOnEditor2.java"><font color="#000055" size=-1>LightBulbLightOnEditor2.java</font></a></td>
</tr>
</table>
<i>
Listing 44.12: Die Klasse LightBulbLightOnEditor2</i></p>
<p>
Der zweite Editor sieht in der Beanbox so aus:
<p>
<a name="imageid044009"></a>
<img src="images/BulbEditor2.gif">
<p>
<p><i>
Abbildung 44.9: LightBulbLightOnEditor2 in der Beanbox</i></p>
<!-- Section -->
<a name="sectlevel4id044006002003"></a>
<h4>LightBulbLightOnEditor3 </h4>
<p>
Zusätzlich zu den beiden einfachen Varianten kann die Bean aber
auch einen vollkommen frei definierten Editor zur Verfügung stellen.
Sie ist in diesem Fall sowohl für die Darstellung der Eigenschaft
in der Eigenschaftenliste als auch für das Verändern des
aktuellen Wertes mit Hilfe einer eigenen Komponente verantwortlich.
Dazu werden weitere Methoden überlagert:
<ul>
<li><a name="ixa103325"><a href="index_i.html#ixb102462"><font color=#000080><tt>isPaintable</tt></font></a></a>
gibt <a href="index_t.html#ixb100233"><font color=#000080><tt>true</tt></font></a>
zurück, um anzuzeigen, dass der Editor selbst für die Darstellung
in der Eigenschaftenliste verantwortlich ist und dafür die Methode
<a name="ixa103326"><a href="index_p.html#ixb102463"><font color=#000080><tt>paintValue</tt></font></a></a>
überlagert.
<li><a name="ixa103327"><a href="index_s.html#ixb102464"><font color=#000080><tt>supportsCustomEditor</tt></font></a></a>
gibt ebenfalls <a href="index_t.html#ixb100233"><font color=#000080><tt>true</tt></font></a>
zurück, um anzuzeigen, dass ein eigener Eigenschafteneditor zur
Verfügung steht und durch Aufruf von <a name="ixa103328"><a href="index_g.html#ixb102465"><font color=#000080><tt>getCustomEditor</tt></font></a></a>
beschafft werden kann. Der eigene Editor muss aus der Klasse <a href="index_c.html#ixb101302"><font color=#000080><tt>Component</tt></font></a>
abgeleitet sein; er wird vom GUI-Designer automatisch in einem Editorfenster
platziert.
</ul>
<p>
Die folgende Klasse <font color="#000077"><tt>LightBulbLightOnEditor3</tt></font>
stellt einen sehr einfachen Editor zur Verfügung. Er besteht
lediglich aus zwei nebeneinanderliegenden Rechtecken in den Farben
Blau und Gelb. Die blaue Farbe symbolisiert den ausgeschalteten Zustand,
die gelbe den eingeschalteten. Der aktuelle Zustand der Lampe wird
durch einen schwarzen Rahmen um eines der beiden Rechtecke angezeigt.
Durch einfaches Anklicken eines der beiden Farbfelder kann der Zustand
umgeschaltet werden.
<a name="listingid044013"></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">/* LightBulbLightOnEditor3.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.beans.*;
<font color="#555555">006 </font>
<font color="#555555">007 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> LightBulbLightOnEditor3
<font color="#555555">008 </font><font color="#0000AA">extends</font> PropertyEditorSupport
<font color="#555555">009 </font>{
<font color="#555555">010 </font> <font color="#006699">boolean</font> currentvalue;
<font color="#555555">011 </font>
<font color="#555555">012 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> setValue(Object value)
<font color="#555555">013 </font> {
<font color="#555555">014 </font> currentvalue = ((Boolean)value).booleanValue();
<font color="#555555">015 </font> }
<font color="#555555">016 </font>
<font color="#555555">017 </font> <font color="#0000AA">public</font> Object getValue()
<font color="#555555">018 </font> {
<font color="#555555">019 </font> <font color="#0000AA">return</font> <font color="#0000AA">new</font> Boolean(currentvalue);
<font color="#555555">020 </font> }
<font color="#555555">021 </font>
<font color="#555555">022 </font> <font color="#0000AA">public</font> <font color="#006699">boolean</font> isPaintable()
<font color="#555555">023 </font> {
<font color="#555555">024 </font> <font color="#0000AA">return</font> <font color="#006699">true</font>;
<font color="#555555">025 </font> }
<font color="#555555">026 </font>
<font color="#555555">027 </font> <font color="#0000AA">public</font> <font color="#006699">boolean</font> supportsCustomEditor()
<font color="#555555">028 </font> {
<font color="#555555">029 </font> <font color="#0000AA">return</font> <font color="#006699">true</font>;
<font color="#555555">030 </font> }
<font color="#555555">031 </font>
<font color="#555555">032 </font> <font color="#0000AA">public</font> Component getCustomEditor()
<font color="#555555">033 </font> {
<font color="#555555">034 </font> <font color="#0000AA">return</font> <font color="#0000AA">new</font> LightOnCustomEditor();
<font color="#555555">035 </font> }
<font color="#555555">036 </font>
<font color="#555555">037 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> paintValue(Graphics g, Rectangle box)
<font color="#555555">038 </font> {
<font color="#555555">039 </font> <font color="#00AA00">//Linke Box: blau, Lampe ausgeschaltet</font>
<font color="#555555">040 </font> g.setColor(Color.blue);
<font color="#555555">041 </font> g.fillRect(box.x, box.y, box.width / 2, box.height);
<font color="#555555">042 </font> <font color="#00AA00">//Rechte Box: blau, Lampe angeschaltet</font>
<font color="#555555">043 </font> g.setColor(Color.yellow);
<font color="#555555">044 </font> g.fillRect(box.x + box.width / 2, box.y, box.width / 2, box.height);
<font color="#555555">045 </font> <font color="#00AA00">//Rahmen</font>
<font color="#555555">046 </font> g.setColor(Color.black);
<font color="#555555">047 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 0; i < 2; ++i) {
<font color="#555555">048 </font> g.drawRect(
<font color="#555555">049 </font> box.x + (currentvalue ? box.width / 2 : 0) + i,
<font color="#555555">050 </font> box.y + i,
<font color="#555555">051 </font> box.width / 2 - 1 - (2 * i),
<font color="#555555">052 </font> box.height - 1 - (2 * i)
<font color="#555555">053 </font> );
<font color="#555555">054 </font> }
<font color="#555555">055 </font> }
<font color="#555555">056 </font>
<font color="#555555">057 </font> <font color="#00AA00">//---Private Klassen----------------------------------------</font>
<font color="#555555">058 </font> <font color="#0000AA">class</font> LightOnCustomEditor
<font color="#555555">059 </font> <font color="#0000AA">extends</font> Canvas
<font color="#555555">060 </font> {
<font color="#555555">061 </font> <font color="#0000AA">public</font> LightOnCustomEditor()
<font color="#555555">062 </font> {
<font color="#555555">063 </font> addMouseListener(
<font color="#555555">064 </font> <font color="#0000AA">new</font> MouseAdapter() {
<font color="#555555">065 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> mouseClicked(MouseEvent event)
<font color="#555555">066 </font> {
<font color="#555555">067 </font> currentvalue = (event.getX() > getSize().width / 2);
<font color="#555555">068 </font> LightBulbLightOnEditor3.<font color="#006699">this</font>.firePropertyChange();
<font color="#555555">069 </font> repaint();
<font color="#555555">070 </font> }
<font color="#555555">071 </font> }
<font color="#555555">072 </font> );
<font color="#555555">073 </font> }
<font color="#555555">074 </font>
<font color="#555555">075 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> paint(Graphics g)
<font color="#555555">076 </font> {
<font color="#555555">077 </font> paintValue(g, <font color="#0000AA">new</font> Rectangle(0, 0, getSize().width, getSize().height));
<font color="#555555">078 </font> }
<font color="#555555">079 </font>
<font color="#555555">080 </font> <font color="#0000AA">public</font> Dimension getPreferredSize()
<font color="#555555">081 </font> {
<font color="#555555">082 </font> <font color="#0000AA">return</font> <font color="#0000AA">new</font> Dimension(120, 60);
<font color="#555555">083 </font> }
<font color="#555555">084 </font> }
<font color="#555555">085 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/LightBulbLightOnEditor3.java"><font color="#000055" size=-1>LightBulbLightOnEditor3.java</font></a></td>
</tr>
</table>
<i>
Listing 44.13: Die Klasse LightBulbLightOnEditor3</i></p>
<p>
Der Editor wird durch die aus <a href="index_c.html#ixb101712"><font color=#000080><tt>Canvas</tt></font></a>
abgeleitete lokale Klasse <font color="#000077"><tt>LightOnCustomEditor</tt></font>
implementiert. Ihre bevorzugte Größe ist fest eingestellt,
und sie verwendet die <a href="index_p.html#ixb102463"><font color=#000080><tt>paintValue</tt></font></a>-Methode
der umgebenden Klasse zur Bildschirmdarstellung. Bei einem Mausklick
verändert sie deren Membervariable <font color="#000077"><tt>currentvalue</tt></font>
und teilt dies dem GUI-Designer durch Aufruf von <a href="index_f.html#ixb102435"><font color=#000080><tt>firePropertyChange</tt></font></a>
mit. Anschließend ruft sie <a href="index_r.html#ixb101509"><font color=#000080><tt>repaint</tt></font></a>
auf, um den Rahmen neu zu zeichnen.
<p>
Der dritte Editor sieht in der Eigenschaftenliste so aus:
<p>
<a name="imageid044010"></a>
<img src="images/BulbEditor3.gif">
<p>
<p><i>
Abbildung 44.10: LightBulbLightOnEditor3 in der Beanbox</i></p>
<p>
Wird er angeklickt, erzeugt die Beanbox eine vergrößerte
Variante, in der der Wert verändert werden kann:
<p>
<a name="imageid044011"></a>
<img src="images/BulbEditor3a.gif">
<p>
<p><i>
Abbildung 44.11: LightBulbLightOnEditor3 in der Eigenschaftenliste</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="k100279.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100284.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100286.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100287.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>
|