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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
|
<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,k100237.html;106,k100237.html;107,k100239.html;108,k100243.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="k100237.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100237.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100239.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100243.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 37 - Swing: Komponenten I
</table>
<hr>
<!-- Section -->
<a name="sectlevel2id037001"></a>
<h2>37.1 Label und Textfelder </h2>
<hr>
<ul>
<li><a href="k100238.html#sectlevel2id037001">37.1 Label und Textfelder</a>
<ul>
<li><a href="k100238.html#jlabel">37.1.1 JLabel</a>
<li><a href="k100238.html#jtextfield">37.1.2 JTextField</a>
<li><a href="k100238.html#sectlevel3id037001003">37.1.3 JPasswordField</a>
<li><a href="k100238.html#sectlevel3id037001004">37.1.4 JTextArea</a>
<li><a href="k100238.html#sectlevel3id037001005">37.1.5 JSpinner</a>
</ul>
</ul>
<hr>
<!-- Section -->
<a name="jlabel"></a>
<h3>37.1.1 JLabel </h3>
<p>
Ein <a name="ixa102561"><a href="index_j.html#ixb101769"><font color=#000080><tt>JLabel</tt></font></a></a>
ist ein Dialogelement zur Anzeige einer Beschriftung innerhalb eines
GUI-Containers. Es besitzt einen Text und ein Icon, die in beliebiger
Anordnung dargestellt werden können. Beide Elemente sind optional,
ein <a href="index_j.html#ixb101769"><font color=#000080><tt>JLabel</tt></font></a>
kann also auch <i>nur</i> ein Icon enthalten. Auf Benutzereingaben
reagiert ein <a href="index_j.html#ixb101769"><font color=#000080><tt>JLabel</tt></font></a>
nicht. Seine wichtigsten Konstruktoren sind:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public JLabel(String text)
public JLabel(Icon image)
public JLabel(String text, Icon icon, int horizontalAlignment)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/swing/JLabel.html" onClick="this.href=getApiDoc('javax.swing.JLabel')"><font color="#660066" size=-1>javax.swing.JLabel</font></a></td>
</tr>
</table>
<p>
Wird ein <a href="index_i.html#ixb101841"><font color=#000080><tt>Icon</tt></font></a>
übergeben, steht es links neben dem Text. Wie es erzeugt werden
kann, wurde in <a href="k100234.html#jmenusonstiges">Abschnitt 36.2.3</a>
gezeigt. Der Parameter <font color="#000077"><tt>horizontalAlignment</tt></font>
gibt an, wie das Label horizontal platziert werden soll, falls links
und rechts mehr Platz als erforderlich zur Verfügung steht. Hier
kann eine der Konstanten <a href="index_l.html#ixb101614"><font color=#000080><tt>LEFT</tt></font></a>,
<a href="index_c.html#ixb101608"><font color=#000080><tt>CENTER</tt></font></a>
oder <a href="index_r.html#ixb101615"><font color=#000080><tt>RIGHT</tt></font></a>
aus dem Interface <a href="index_s.html#ixb101847"><font color=#000080><tt>SwingConstants</tt></font></a>
angegeben werden.
<p>
Auf die relative Anordnung von Text und Icon kann mit den Methoden
<a name="ixa102562"><a href="index_s.html#ixb101844"><font color=#000080><tt>setHorizontalTextPosition</tt></font></a></a>
und <a name="ixa102563"><a href="index_g.html#ixb101903"><font color=#000080><tt>getHorizontalTextPosition</tt></font></a></a>
zugegriffen werden (standardmäßig steht der Text rechts
neben dem Icon). Mögliche Werte sind <a href="index_l.html#ixb101614"><font color=#000080><tt>LEFT</tt></font></a>
und <a href="index_r.html#ixb101615"><font color=#000080><tt>RIGHT</tt></font></a>.
:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void setHorizontalTextPosition(int textPosition)
public int getHorizontalTextPosition()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/swing/JLabel.html" onClick="this.href=getApiDoc('javax.swing.JLabel')"><font color="#660066" size=-1>javax.swing.JLabel</font></a></td>
</tr>
</table>
<p>
<a href="index_j.html#ixb101769"><font color=#000080><tt>JLabel</tt></font></a>
stellt noch einige Methoden zur Verfügung, mit denen die Anordnung
seines Inhalts beeinflusst 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 void setHorizontalAlignment(int alignment)
public int getHorizontalAlignment()
public void setVerticalAlignment(int alignment)
public int getVerticalAlignment()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/swing/JLabel.html" onClick="this.href=getApiDoc('javax.swing.JLabel')"><font color="#660066" size=-1>javax.swing.JLabel</font></a></td>
</tr>
</table>
<p>
Mit <a name="ixa102564"><a href="index_s.html#ixb101904"><font color=#000080><tt>setHorizontalAlignment</tt></font></a></a>
wird die horizontale Ausrichtung festgelegt, mögliche Parameter
sind <a href="index_l.html#ixb101614"><font color=#000080><tt>LEFT</tt></font></a>,
<a href="index_c.html#ixb101608"><font color=#000080><tt>CENTER</tt></font></a>
oder <a href="index_r.html#ixb101615"><font color=#000080><tt>RIGHT</tt></font></a>.
Mit <a name="ixa102565"><a href="index_s.html#ixb101905"><font color=#000080><tt>setVerticalAlignment</tt></font></a></a>
wird die vertikale Ausrichtung festgelegt, hier kann eine der Konstanten
<a href="index_t.html#ixb101853"><font color=#000080><tt>TOP</tt></font></a>,
<a href="index_c.html#ixb101608"><font color=#000080><tt>CENTER</tt></font></a>
oder <a href="index_b.html#ixb101848"><font color=#000080><tt>BOTTOM</tt></font></a>
aus dem Interface <a href="index_s.html#ixb101847"><font color=#000080><tt>SwingConstants</tt></font></a>
übergeben werden. Mit <a name="ixa102566"><a href="index_g.html#ixb101906"><font color=#000080><tt>getHorizontalAlignment</tt></font></a></a>
und <a name="ixa102567"><a href="index_g.html#ixb101907"><font color=#000080><tt>getVerticalAlignment</tt></font></a></a>
können beide Einstellungen auch abgefragt werden.
<p>
Das folgende Programm erzeugt einige Labels mit unterschiedlichen
Eigenschaften:
<a name="listingid037001"></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">/* Listing3701.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> javax.swing.*;
<font color="#555555">005 </font>
<font color="#555555">006 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing3701
<font color="#555555">007 </font><font color="#0000AA">extends</font> JFrame
<font color="#555555">008 </font>{
<font color="#555555">009 </font> <font color="#0000AA">public</font> Listing3701()
<font color="#555555">010 </font> {
<font color="#555555">011 </font> <font color="#006699">super</font>(<font color="#0000FF">"JLabel"</font>);
<font color="#555555">012 </font> addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">013 </font> Container cp = getContentPane();
<font color="#555555">014 </font> cp.setLayout(<font color="#0000AA">new</font> GridLayout(5, 1));
<font color="#555555">015 </font> JLabel label;
<font color="#555555">016 </font> <font color="#00AA00">//Standardlabel</font>
<font color="#555555">017 </font> label = <font color="#0000AA">new</font> JLabel(<font color="#0000FF">"Standard-Label"</font>);
<font color="#555555">018 </font> cp.add(label);
<font color="#555555">019 </font> <font color="#00AA00">//Label mit Icon</font>
<font color="#555555">020 </font> label = <font color="#0000AA">new</font> JLabel(
<font color="#555555">021 </font> <font color="#0000FF">"Label mit Icon"</font>,
<font color="#555555">022 </font> <font color="#0000AA">new</font> ImageIcon(<font color="#0000FF">"lock.gif"</font>),
<font color="#555555">023 </font> JLabel.CENTER
<font color="#555555">024 </font> );
<font color="#555555">025 </font> cp.add(label);
<font color="#555555">026 </font> <font color="#00AA00">//Nur-Icon</font>
<font color="#555555">027 </font> label = <font color="#0000AA">new</font> JLabel(<font color="#0000AA">new</font> ImageIcon(<font color="#0000FF">"lock.gif"</font>));
<font color="#555555">028 </font> cp.add(label);
<font color="#555555">029 </font> <font color="#00AA00">//Icon auf der rechten Seite</font>
<font color="#555555">030 </font> label = <font color="#0000AA">new</font> JLabel(
<font color="#555555">031 </font> <font color="#0000FF">"Label mit Icon rechts"</font>,
<font color="#555555">032 </font> <font color="#0000AA">new</font> ImageIcon(<font color="#0000FF">"lock.gif"</font>),
<font color="#555555">033 </font> JLabel.CENTER
<font color="#555555">034 </font> );
<font color="#555555">035 </font> label.setHorizontalTextPosition(JLabel.LEFT);
<font color="#555555">036 </font> cp.add(label);
<font color="#555555">037 </font> <font color="#00AA00">//Label rechts unten</font>
<font color="#555555">038 </font> label = <font color="#0000AA">new</font> JLabel(<font color="#0000FF">"Label rechts unten"</font>);
<font color="#555555">039 </font> label.setHorizontalAlignment(JLabel.RIGHT);
<font color="#555555">040 </font> label.setVerticalAlignment(JLabel.BOTTOM);
<font color="#555555">041 </font> cp.add(label);
<font color="#555555">042 </font> }
<font color="#555555">043 </font>
<font color="#555555">044 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">045 </font> {
<font color="#555555">046 </font> Listing3701 frame = <font color="#0000AA">new</font> Listing3701();
<font color="#555555">047 </font> frame.setLocation(100, 100);
<font color="#555555">048 </font> frame.setSize(300, 200);
<font color="#555555">049 </font> frame.setVisible(<font color="#006699">true</font>);
<font color="#555555">050 </font> }
<font color="#555555">051 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing3701.java"><font color="#000055" size=-1>Listing3701.java</font></a></td>
</tr>
</table>
<i>
Listing 37.1: Die Klasse JLabel</i></p>
<p>
Die Ausgabe des Programms ist:
<p>
<a name="imageid037001"></a>
<img src="images/JLabel1.gif">
<p>
<p><i>
Abbildung 37.1: Die Klasse JLabel</i></p>
<!-- Section -->
<a name="jtextfield"></a>
<h3>37.1.2 JTextField </h3>
<p>
Die Klasse <a name="ixa102568"><a href="index_j.html#ixb101770"><font color=#000080><tt>JTextField</tt></font></a></a>
ist das Swing-Pendant zur AWT-Klasse <a href="index_t.html#ixb101473"><font color=#000080><tt>TextField</tt></font></a>
(siehe <a href="k100212.html#textfield">Abschnitt 32.6</a>) und stellt
ein einzeiliges Textfeld zur Eingabe von Daten dar. In ihren grundlegenden
Möglichkeiten ist die Programmierschnittstelle beider Klassen
ähnlich, <a href="index_j.html#ixb101770"><font color=#000080><tt>JTextField</tt></font></a>
bietet aber zusätzliche Möglichkeiten, die in <a href="index_t.html#ixb101473"><font color=#000080><tt>TextField</tt></font></a>
nicht zu finden sind. Ein Großteil der Fähigkeiten von
<a href="index_j.html#ixb101770"><font color=#000080><tt>JTextField</tt></font></a>
ist in der Basisklasse <a name="ixa102569"><a href="index_j.html#ixb101908"><font color=#000080><tt>JTextComponent</tt></font></a></a>
des Pakets <a name="ixa102570"><a href="index_j.html#ixb101909"><font color=#000080><tt>javax.swing.text</tt></font></a></a>
implementiert.
<p>
Die wichtigsten Konstruktoren von <a href="index_j.html#ixb101770"><font color=#000080><tt>JTextField</tt></font></a>
sind:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public JTextField(int columns)
public JTextField(String text)
public JTextField(String text, int columns)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/swing/JTextField.html" onClick="this.href=getApiDoc('javax.swing.JTextField')"><font color="#660066" size=-1>javax.swing.JTextField</font></a></td>
</tr>
</table>
<p>
Der erste von ihnen erzeugt ein leeres Textfeld mit der angegebenen
Anzahl Spalten, der zweite ein Textfeld mit dem angegebenen Text.
Beim dritten Konstruktor können sowohl Spaltenzahl als auch Text
vorgegeben werden. Die Spaltenzahl wird zur Berechnung der Breite
des Textfeldes verwendet (Anzahl Spalten mal Breite eines »m«),
sie dient nicht dazu, die Anzahl der Eingabezeichen zu begrenzen.
Ist die Spaltenzahl 0 wie im zweiten Konstruktor, wird die initiale
Textlänge zur Berechnung der Spaltenbreite verwendet.
<p>
<a href="index_j.html#ixb101770"><font color=#000080><tt>JTextField</tt></font></a>
bietet ähnliche Methoden wie <a href="index_t.html#ixb101473"><font color=#000080><tt>TextField</tt></font></a>:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public String getText()
public void setText(String t)
public String getText(int offs, int len)
public String getSelectedText()
public int getSelectionStart()
public int getSelectionEnd()
public void setSelectionStart(int selectionStart)
public void setSelectionEnd(int selectionEnd)
public int getCaretPosition()
public void setCaretPosition(int pos)
public void moveCaretPosition(int pos)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/swing/JTextField.html" onClick="this.href=getApiDoc('javax.swing.JTextField')"><font color="#660066" size=-1>javax.swing.JTextField</font></a></td>
</tr>
</table>
<p>
Mit den parameterlosen Versionen von <a name="ixa102571"><a href="index_g.html#ixb101645"><font color=#000080><tt>getText</tt></font></a></a>
und <a name="ixa102572"><a href="index_s.html#ixb101644"><font color=#000080><tt>setText</tt></font></a></a>
kann auf den kompletten Inhalt des Textfelds zugegriffen werden. Die
parametrisierte Variante von <a href="index_g.html#ixb101645"><font color=#000080><tt>getText</tt></font></a>
liefert den Textausschnitt der angegebenen Länge und Position.
<a name="ixa102573"><a href="index_g.html#ixb101660"><font color=#000080><tt>getSelectedText</tt></font></a></a>
liefert dagegen den selektierten Text (bzw. <a href="index_n.html#ixb100235"><font color=#000080><tt>null</tt></font></a>,
wenn kein Text selektiert ist), und mit <a name="ixa102574"><a href="index_g.html#ixb101658"><font color=#000080><tt>getSelectionStart</tt></font></a></a>
und <a name="ixa102575"><a href="index_g.html#ixb101659"><font color=#000080><tt>getSelectionEnd</tt></font></a></a>
kann Anfang und Ende des selektierten Bereichs ermittelt werden. Mit
<a name="ixa102576"><a href="index_s.html#ixb101910"><font color=#000080><tt>setSelectionStart</tt></font></a></a>
und <a name="ixa102577"><a href="index_s.html#ixb101911"><font color=#000080><tt>setSelectionEnd</tt></font></a></a>
kann dieser sogar verändert werden. <a name="ixa102578"><a href="index_g.html#ixb101661"><font color=#000080><tt>getCaretPosition</tt></font></a></a>
liefert die Position der Eingabemarke, und mit <a name="ixa102579"><a href="index_s.html#ixb101662"><font color=#000080><tt>setCaretPosition</tt></font></a></a>
kann sie verändert werden. <a name="ixa102580"><a href="index_m.html#ixb101912"><font color=#000080><tt>moveCaretPosition</tt></font></a></a>
verändert die Position der Einfügemarke und markiert dabei
den Bereich zwischen alter und neuer Position. Für alle Positionsangaben
gilt, dass der Platz vor dem ersten Zeichen die Position 0 und der
nach dem letzten Textzeichen die Position <i>Länge des Textes</i>
hat.
<p>
Die wichtigsten registrierbaren Listener sind <a href="index_a.html#ixb101469"><font color=#000080><tt>ActionListener</tt></font></a>
und <a name="ixa102581"><a href="index_c.html#ixb101913"><font color=#000080><tt>CaretListener</tt></font></a></a>:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public void addActionListener(ActionListener l)
public void addCaretListener(CaretListener listener)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/swing/JTextField.html" onClick="this.href=getApiDoc('javax.swing.JTextField')"><font color="#660066" size=-1>javax.swing.JTextField</font></a></td>
</tr>
</table>
<p>
Ein <a href="index_a.html#ixb101469"><font color=#000080><tt>ActionListener</tt></font></a>
wird immer dann aufgerufen, wenn im Eingabefeld <font color="#007700"><tt>[ENTER]</tt></font>
gedrückt wird, ein <a href="index_c.html#ixb101913"><font color=#000080><tt>CaretListener</tt></font></a>,
wenn sich die Position der Einfügemarke geändert hat.
<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>
Einen <a href="index_t.html#ixb101487"><font color=#000080><tt>TextListener</tt></font></a>
wie bei der Klasse <a href="index_t.html#ixb101473"><font color=#000080><tt>TextField</tt></font></a>
gibt es bei den aus <a href="index_j.html#ixb101908"><font color=#000080><tt>JTextComponent</tt></font></a>
abgeleiteten Klassen nicht. Will die Anwendung über jede Änderung
in einem Textfeld unterrichtet werden, muss sie einen <a name="ixa102582"><a href="index_d.html#ixb101914"><font color=#000080><tt>DocumentListener</tt></font></a></a>
auf dessen Modell registrieren. Das Modell eines Textfeldes ist eine
Instanz der Klasse <a name="ixa102583"><a href="index_d.html#ixb101915"><font color=#000080><tt>Document</tt></font></a></a>
und kann durch Aufruf von <a name="ixa102584"><a href="index_g.html#ixb101916"><font color=#000080><tt>getDocument</tt></font></a></a>
beschafft werden.</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>
<p>
Das folgende Beispiel zeigt ein Programm mit drei Textfeldern. Auf
dem untersten sind ein <a href="index_a.html#ixb101469"><font color=#000080><tt>ActionListener</tt></font></a>
und ein <a href="index_c.html#ixb101913"><font color=#000080><tt>CaretListener</tt></font></a>
registriert, die bei jedem Aufruf einige Parameter auf der Konsole
ausgeben:
<a name="listingid037002"></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">/* Listing3702.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> javax.swing.*;
<font color="#555555">006 </font><font color="#0000AA">import</font> javax.swing.event.*;
<font color="#555555">007 </font>
<font color="#555555">008 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing3702
<font color="#555555">009 </font><font color="#0000AA">extends</font> JFrame
<font color="#555555">010 </font><font color="#0000AA">implements</font> ActionListener, CaretListener
<font color="#555555">011 </font>{
<font color="#555555">012 </font> <font color="#0000AA">public</font> Listing3702()
<font color="#555555">013 </font> {
<font color="#555555">014 </font> <font color="#006699">super</font>(<font color="#0000FF">"JTextField"</font>);
<font color="#555555">015 </font> addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">016 </font> Container cp = getContentPane();
<font color="#555555">017 </font> cp.setLayout(<font color="#0000AA">new</font> FlowLayout());
<font color="#555555">018 </font> JTextField tf;
<font color="#555555">019 </font> <font color="#00AA00">//Linksbündiges Textfeld mit "Hello, world"</font>
<font color="#555555">020 </font> tf = <font color="#0000AA">new</font> JTextField(<font color="#0000FF">"Hello, world"</font>);
<font color="#555555">021 </font> cp.add(tf);
<font color="#555555">022 </font> <font color="#00AA00">//Leeres Textfeld mit 20 Spalten</font>
<font color="#555555">023 </font> tf = <font color="#0000AA">new</font> JTextField(20);
<font color="#555555">024 </font> cp.add(tf);
<font color="#555555">025 </font> <font color="#00AA00">//Textfeld mit "Hello, world" und 20 Spalten</font>
<font color="#555555">026 </font> tf = <font color="#0000AA">new</font> JTextField(<font color="#0000FF">"Hello, world"</font>, 20);
<font color="#555555">027 </font> tf.addActionListener(<font color="#006699">this</font>);
<font color="#555555">028 </font> tf.addCaretListener(<font color="#006699">this</font>);
<font color="#555555">029 </font> cp.add(tf);
<font color="#555555">030 </font> }
<font color="#555555">031 </font>
<font color="#555555">032 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> actionPerformed(ActionEvent event)
<font color="#555555">033 </font> {
<font color="#555555">034 </font> JTextField tf = (JTextField)event.getSource();
<font color="#555555">035 </font> System.out.println(<font color="#0000FF">"---ActionEvent---"</font>);
<font color="#555555">036 </font> System.out.println(tf.getText());
<font color="#555555">037 </font> System.out.println(tf.getSelectedText());
<font color="#555555">038 </font> System.out.println(tf.getSelectionStart());
<font color="#555555">039 </font> System.out.println(tf.getSelectionEnd());
<font color="#555555">040 </font> System.out.println(tf.getCaretPosition());
<font color="#555555">041 </font> }
<font color="#555555">042 </font>
<font color="#555555">043 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> caretUpdate(CaretEvent event)
<font color="#555555">044 </font> {
<font color="#555555">045 </font> System.out.println(<font color="#0000FF">"---CaretEvent---"</font>);
<font color="#555555">046 </font> System.out.println(event.getDot());
<font color="#555555">047 </font> System.out.println(event.getMark());
<font color="#555555">048 </font> }
<font color="#555555">049 </font>
<font color="#555555">050 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">051 </font> {
<font color="#555555">052 </font> Listing3702 frame = <font color="#0000AA">new</font> Listing3702();
<font color="#555555">053 </font> frame.setLocation(100, 100);
<font color="#555555">054 </font> frame.setSize(300, 150);
<font color="#555555">055 </font> frame.setVisible(<font color="#006699">true</font>);
<font color="#555555">056 </font> }
<font color="#555555">057 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing3702.java"><font color="#000055" size=-1>Listing3702.java</font></a></td>
</tr>
</table>
<i>
Listing 37.2: Die Klasse JTextField</i></p>
<p>
Die Ausgabe des Programms ist:
<p>
<a name="imageid037002"></a>
<img src="images/JTextField1.gif">
<p>
<p><i>
Abbildung 37.2: Die Klasse JTextField</i></p>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Seit dem JDK 1.4 gibt es eine aus <a href="index_j.html#ixb101770"><font color=#000080><tt>JTextField</tt></font></a>
abgeleitete Klasse <a name="ixa102585"><a href="index_j.html#ixb100001"><font color=#000080><tt>JFormattedTextField</tt></font></a></a>
zum Erfassen <i>formatierter</i> Strings. Sie kann dazu verwendet
werden, Zahlen, Datumswerte oder andere formatierte Strings einzugeben,
sie während der Eingabe zu validieren und später korrekt
formatiert anzuzeigen. Eine interessante Eigenschaft dieser Klasse
ist, dass sich einstellen läßt, wie sie auf einen Fokusverlust
(also das Verlassen des Textfelds durch den Anwender) reagieren soll.
Es ist beispielsweise möglich, korrekte Eingaben automatisch
zu akzeptieren, bei fehlerhaften aber auf den alten Wert zurückzugehen.
So lassen sich mit relativ wenig Aufwand einfache Validierungsmechanismen
für Textfelder erzeugen.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#FF9900">
<tr>
<td><font color="#FFFFFF"> JDK1.1-6.0 </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
</tr>
</table>
<!-- Section -->
<a name="sectlevel3id037001003"></a>
<h3>37.1.3 JPasswordField </h3>
<p>
<a name="ixa102586"><a href="index_j.html#ixb101771"><font color=#000080><tt>JPasswordField</tt></font></a></a>
ist eine Spezialisierung von <a href="index_j.html#ixb101770"><font color=#000080><tt>JTextField</tt></font></a>
zur Eingabe von Passwörtern. Der Hauptunterschied zu <a href="index_j.html#ixb101770"><font color=#000080><tt>JTextField</tt></font></a>
besteht darin, dass der eingegebene Text nicht angezeigt, sondern
statt dessen für jedes Zeichen ein Sternchen ausgegeben wird.
Durch Aufruf von <a name="ixa102587"><a href="index_s.html#ixb101917"><font color=#000080><tt>setEchoChar</tt></font></a></a>
kann auch ein anderes Ausgabezeichen ausgewählt werden. Des weiteren
sind die beiden Zwischenablagefunktionen <i>Kopieren</i> und <i>Ausschneiden</i>
deaktiviert, um zu verhindern, dass der Text aus einem Paßwortfeld
in ein anderes Feld kopiert oder an ein anderes Programm übergeben
werden kann.
<!-- Section -->
<a name="sectlevel3id037001004"></a>
<h3>37.1.4 JTextArea </h3>
<p>
<a name="ixa102588"><a href="index_j.html#ixb101918"><font color=#000080><tt>JTextArea</tt></font></a></a>
ist eine Komponente zur Anzeige und Eingabe von mehrzeiligen Texten.
Wie die AWT-Klasse <a href="index_t.html#ixb101489"><font color=#000080><tt>TextArea</tt></font></a>
dient sie dazu, <i>unformatierte</i> Texte zu bearbeiten. Diese können
zwar Zeilenumbrüche und Tabulatoren, nicht aber unterschiedliche
Schriften, Farben oder grafische Elemente enthalten (für diesen
Zweck gibt es die Klassen <a name="ixa102589"><a href="index_j.html#ixb101919"><font color=#000080><tt>JEditorPane</tt></font></a></a>
und <a name="ixa102590"><a href="index_j.html#ixb101920"><font color=#000080><tt>JTextPane</tt></font></a></a>,
die hier nicht behandelt werden sollen). Die wichtigsten Konstruktoren
von <a href="index_j.html#ixb101918"><font color=#000080><tt>JTextArea</tt></font></a>
sind:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public JTextArea(String text)
public JTextArea(int rows, int columns)
public JTextArea(String text, int rows, int columns)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/swing/JTextArea.html" onClick="this.href=getApiDoc('javax.swing.JTextArea')"><font color="#660066" size=-1>javax.swing.JTextArea</font></a></td>
</tr>
</table>
<p>
Sie entsprechen im Prinzip denen von <a href="index_j.html#ixb101770"><font color=#000080><tt>JTextField</tt></font></a>,
zusätzlich kann jedoch die Anzahl der Zeilen angegeben werden.
Neben den in <a href="k100238.html#jtextfield">Abschnitt 37.1.2</a>
vorgestellten Methoden der Klasse <a href="index_j.html#ixb101770"><font color=#000080><tt>JTextField</tt></font></a>
stehen zusätzliche Methoden zum zeilenweisen Zugriff auf den
dargestellten Text zur Verfügung:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public int getLineCount()
public int getLineStartOffset(int line)
public int getLineEndOffset(int line)
public int getLineOfOffset(int offset)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/swing/JTextArea.html" onClick="this.href=getApiDoc('javax.swing.JTextArea')"><font color="#660066" size=-1>javax.swing.JTextArea</font></a></td>
</tr>
</table>
<p>
<a name="ixa102591"><a href="index_g.html#ixb101921"><font color=#000080><tt>getLineCount</tt></font></a></a>
liefert die Gesamtzahl der Zeilen. Mit <a name="ixa102592"><a href="index_g.html#ixb101922"><font color=#000080><tt>getLineStartOffset</tt></font></a></a>
und <a name="ixa102593"><a href="index_g.html#ixb101923"><font color=#000080><tt>getLineEndOffset</tt></font></a></a>
kann zu einer beliebigen Zeile ihr Anfangs- und Endeoffset bestimmt
werden. Mit diesen Informationen kann beispielsweise <a href="index_g.html#ixb101645"><font color=#000080><tt>getText</tt></font></a>
aufgerufen werden, um den Inhalt einer bestimmten Zeile zu lesen.
Die Methode <a name="ixa102594"><a href="index_g.html#ixb101924"><font color=#000080><tt>getLineOfOffset</tt></font></a></a>
dient dazu, die Nummer einer Zeile zu ermitteln, wenn der Offset eines
darin enthaltenen Zeichens bekannt ist.
<p>
<a href="index_j.html#ixb101918"><font color=#000080><tt>JTextArea</tt></font></a>
stellt einige Methoden zur Verfügung, mit denen die Formatierung
des Textes beeinflusst 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 void setTabSize(int size)
public int getTabSize()
public void setLineWrap(boolean wrap)
public boolean getLineWrap()
public void setWrapStyleWord(boolean word)
public boolean getWrapStyleWord()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/swing/JTextArea.html" onClick="this.href=getApiDoc('javax.swing.JTextArea')"><font color="#660066" size=-1>javax.swing.JTextArea</font></a></td>
</tr>
</table>
<p>
Mit <a name="ixa102595"><a href="index_s.html#ixb101925"><font color=#000080><tt>setTabSize</tt></font></a></a>
und <a name="ixa102596"><a href="index_g.html#ixb101926"><font color=#000080><tt>getTabSize</tt></font></a></a>
kann auf die Tabulatorweite zugegriffen werden. Sie hat standardmäßig
den Wert 8 (zur Umrechnung in Bildschirmpixel wird er mit der Breite
des breitesten Buchstabens im aktuellen Font multipliziert). Mit <a name="ixa102597"><a href="index_s.html#ixb101927"><font color=#000080><tt>setLineWrap</tt></font></a></a>
kann festgelegt werden, ob zu breite Spalten automatisch umbrochen
werden sollen. <a name="ixa102598"><a href="index_s.html#ixb101928"><font color=#000080><tt>setWrapStyleWord</tt></font></a></a>
definiert, ob dies an Wortgrenzen oder mitten im Wort geschehen soll.
Mit <a name="ixa102599"><a href="index_g.html#ixb101929"><font color=#000080><tt>getLineWrap</tt></font></a></a>
und <a name="ixa102600"><a href="index_g.html#ixb101930"><font color=#000080><tt>getWrapStyleWord</tt></font></a></a>
können die beiden Eigenschaften auch abgefragt 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>
Anders als <a href="index_t.html#ixb101489"><font color=#000080><tt>TextArea</tt></font></a>
ist <a href="index_j.html#ixb101918"><font color=#000080><tt>JTextArea</tt></font></a>
nicht in der Lage, Text automatisch zu scrollen, wenn er zu lang oder
zu breit für den verfügbaren Bildschirmausschnitt ist. Wird
das gewünscht, muss das Textfeld in eine Komponente des Typs
<a name="ixa102601"><a href="index_j.html#ixb101779"><font color=#000080><tt>JScrollPane</tt></font></a></a>
eingebettet werden. An Stelle der <a href="index_j.html#ixb101918"><font color=#000080><tt>JTextArea</tt></font></a>
ist dem Dialog dann die <a href="index_j.html#ixb101779"><font color=#000080><tt>JScrollPane</tt></font></a>
hinzuzufügen. Weitere Informationen zu <a href="index_j.html#ixb101779"><font color=#000080><tt>JScrollPane</tt></font></a>
finden sich in <a href="k100244.html#jscrollpane">Abschnitt 38.1.1</a>.</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>
Das folgende Programm enthält eine <a href="index_j.html#ixb101918"><font color=#000080><tt>JTextArea</tt></font></a>
mit 30 Spalten und 20 Zeilen. Ihre Tabulatorweite wurde auf 4 reduziert,
zu lange Zeilen werden automatisch an Wortgrenzen umbrochen. Die <a href="index_j.html#ixb101918"><font color=#000080><tt>JTextArea</tt></font></a>
wurde in eine <a href="index_j.html#ixb101779"><font color=#000080><tt>JScrollPane</tt></font></a>
eingebettet und erhält - wenn nötig - automatisch einen
vertikalen Schieber. Ein horizontaler Schieber wird nicht verwendet,
da die Zeilen wegen des automatischen Umbruchs nicht zu lang werden
können.
<a name="listingid037003"></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">/* Listing3703.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> javax.swing.*;
<font color="#555555">005 </font>
<font color="#555555">006 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing3703
<font color="#555555">007 </font><font color="#0000AA">extends</font> JFrame
<font color="#555555">008 </font>{
<font color="#555555">009 </font> <font color="#0000AA">public</font> Listing3703()
<font color="#555555">010 </font> {
<font color="#555555">011 </font> <font color="#006699">super</font>(<font color="#0000FF">"JTextArea"</font>);
<font color="#555555">012 </font> addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">013 </font> Container cp = getContentPane();
<font color="#555555">014 </font> JTextArea ta = <font color="#0000AA">new</font> JTextArea(<font color="#0000FF">"Hello, world"</font>, 20, 30);
<font color="#555555">015 </font> ta.setTabSize(4);
<font color="#555555">016 </font> ta.setLineWrap(<font color="#006699">true</font>);
<font color="#555555">017 </font> ta.setWrapStyleWord(<font color="#006699">true</font>);
<font color="#555555">018 </font> cp.add(<font color="#0000AA">new</font> JScrollPane(ta));
<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> <font color="#006699">void</font> main(String[] args)
<font color="#555555">022 </font> {
<font color="#555555">023 </font> Listing3703 frame = <font color="#0000AA">new</font> Listing3703();
<font color="#555555">024 </font> frame.setLocation(100, 100);
<font color="#555555">025 </font> frame.setSize(300, 200);
<font color="#555555">026 </font> frame.setVisible(<font color="#006699">true</font>);
<font color="#555555">027 </font> }
<font color="#555555">028 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing3703.java"><font color="#000055" size=-1>Listing3703.java</font></a></td>
</tr>
</table>
<i>
Listing 37.3: Die Klasse JTextArea</i></p>
<p>
Die Ausgabe des Programms ist:
<p>
<a name="imageid037003"></a>
<img src="images/JTextArea1.gif">
<p>
<p><i>
Abbildung 37.3: Die Klasse JTextArea</i></p>
<!-- Section -->
<a name="sectlevel3id037001005"></a>
<h3>37.1.5 JSpinner </h3>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Seit dem JDK 1.4 gibt es die Klasse <a name="ixa102602"><a href="index_j.html#ixb100002"><font color=#000080><tt>JSpinner</tt></font></a></a>,
mit der Textfelder erzeugt werden können, deren Inhalt einer
vordefinierten, geordneten Liste von Werten entstammt. Diese Werte
können vom Anwender nicht nur manuell eingegeben werden, sondern
auch auf- oder absteigend durchlaufen werden. Dazu besitzt der <a href="index_j.html#ixb100002"><font color=#000080><tt>JSpinner</tt></font></a>
zwei kleine Buttons, mit denen die Wertefolge abgerufen werden kann.
Alternativ können meist auch die Tasten <font color="#007700"><tt>[CURSORHOCH]</tt></font>
oder <font color="#007700"><tt>[CURSORRUNTER]</tt></font> verwendet
werden, wenn der <a href="index_j.html#ixb100002"><font color=#000080><tt>JSpinner</tt></font></a>
den Fokus hat.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#FF9900">
<tr>
<td><font color="#FFFFFF"> JDK1.1-6.0 </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
</tr>
</table>
<p>
Konzeptionell kann ein Spinner als Mischung aus Texfeldern und den
in <a href="k100240.html#listenundcomboboxen">Abschnitt 37.3</a> vorgestellten
Listen oder Comboboxen angesehen werden. Im Gegensatz zu Listen und
Comboboxen muss der Wertevorrat eines Spinners allerdings nicht notwendigerweise
<i>endlich</i> sein. Soll beispielsweise ein Datum eingegeben werden,
kann es sinnvoll sein, den Wertebereich nach unten oder oben offen
zu lassen, um beliebige Datumswerte eingeben zu können. Gegenüber
einer Combobox hat ein Spinner zudem den Vorteil, dass er bei der
Darstellung auf dem Bildschirm keine darunterliegenden Dialogelemente
verdecken kann. Nachteilig ist allerdings, dass es keine Möglichkeit
der Vorschau auf die Menge der insgesamt verfügbaren Elemente
gibt.
<p>
Die Klasse <a href="index_j.html#ixb100002"><font color=#000080><tt>JSpinner</tt></font></a>
besitzt zwei Konstruktoren:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public JSpinner()
public JSpinner(SpinnerModel model)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/javax/swing/JSpinner.html" onClick="this.href=getApiDoc('javax.swing.JSpinner')"><font color="#660066" size=-1>javax.swing.JSpinner</font></a></td>
</tr>
</table>
<p>
Der erste erzeugt einen <a href="index_j.html#ixb100002"><font color=#000080><tt>JSpinner</tt></font></a>
für Ganzzahlen, die in Einerschritten beliebig weit in jede Richtung
durchlaufen werden können. Der zweite Konstruktor erwartet ein
<a name="ixa102603"><a href="index_s.html#ixb101931"><font color=#000080><tt>SpinnerModel</tt></font></a></a>,
mit dem der Typ der darzustellenden Daten festgelegt wird. <a href="index_s.html#ixb101931"><font color=#000080><tt>SpinnerModel</tt></font></a>
ist ein Interface im Paket <a href="index_j.html#ixb100192"><font color=#000080><tt>javax.swing</tt></font></a>,
das die Wertemenge eines Spinners über dessen aktuelles Element
sowie den jeweiligen Vorgänger und Nachfolger dieses Elements
definiert. Zu diesem Interface gibt es eine abstrakte Implementierung
<a name="ixa102604"><a href="index_a.html#ixb101932"><font color=#000080><tt>AbstractSpinnerModel</tt></font></a></a>
und drei Konkretisierungen <a name="ixa102605"><a href="index_s.html#ixb101933"><font color=#000080><tt>SpinnerDateModel</tt></font></a></a>,
<a name="ixa102606"><a href="index_s.html#ixb101934"><font color=#000080><tt>SpinnerNumberModel</tt></font></a></a>
und <a name="ixa102607"><a href="index_s.html#ixb101935"><font color=#000080><tt>SpinnerListModel</tt></font></a></a>.
Sie repräsentieren Folgen von Datumswerten, Ganz- oder Fließkommazahlen
oder fest vorgegebene Listen von Strings.
<p>
Um einen <a href="index_j.html#ixb100002"><font color=#000080><tt>JSpinner</tt></font></a>
zu erzeugen, wird das gewünschte Modell instanziert und an den
Konstruktor der Klasse übergeben. Das folgende Beispiel zeigt
ein einfaches Fenster mit vier Spinnern. Der erste Spinner repräsentiert
eine nach oben und unten offene Liste von Ganzzahlen der Schrittweite
1. Beim zweiten ist ihr Wertebereich begrenzt, und die Folge hat die
Schrittweite 7. Der dritte Spinner erlaubt die Auswahl von Datumswerten
und der vierte die von Wochentagen.
<a name="listingid037004"></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">/* Listing3704.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> javax.swing.*;
<font color="#555555">006 </font><font color="#0000AA">import</font> javax.swing.event.*;
<font color="#555555">007 </font>
<font color="#555555">008 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing3704
<font color="#555555">009 </font><font color="#0000AA">extends</font> JFrame
<font color="#555555">010 </font>{
<font color="#555555">011 </font> <font color="#0000AA">private</font> <font color="#0000AA">static</font> <font color="#0000AA">final</font> String[] WDAYS = {
<font color="#555555">012 </font> <font color="#0000FF">"Montag"</font>, <font color="#0000FF">"Dienstag"</font>, <font color="#0000FF">"Mittwoch"</font>, <font color="#0000FF">"Donnerstag"</font>,
<font color="#555555">013 </font> <font color="#0000FF">"Freitag"</font>, <font color="#0000FF">"Samstag"</font>, <font color="#0000FF">"Sonntag"</font>
<font color="#555555">014 </font> };
<font color="#555555">015 </font>
<font color="#555555">016 </font> <font color="#0000AA">public</font> Listing3704()
<font color="#555555">017 </font> {
<font color="#555555">018 </font> <font color="#006699">super</font>(<font color="#0000FF">"JSpinner"</font>);
<font color="#555555">019 </font> addWindowListener(<font color="#0000AA">new</font> WindowClosingAdapter(<font color="#006699">true</font>));
<font color="#555555">020 </font> Container cp = getContentPane();
<font color="#555555">021 </font> cp.setLayout(<font color="#0000AA">new</font> FlowLayout());
<font color="#555555">022 </font> <font color="#00AA00">//Default-Spinner für Ganzzahlen</font>
<font color="#555555">023 </font> JSpinner spinner = <font color="#0000AA">new</font> JSpinner();
<font color="#555555">024 </font> cp.add(spinner);
<font color="#555555">025 </font> <font color="#00AA00">//Spinner für einige Vielfache von 7</font>
<font color="#555555">026 </font> spinner = <font color="#0000AA">new</font> JSpinner(<font color="#0000AA">new</font> SpinnerNumberModel(91, 49, 126, 7));
<font color="#555555">027 </font> cp.add(spinner);
<font color="#555555">028 </font> <font color="#00AA00">//Spinner für Datum/Uhrzeit</font>
<font color="#555555">029 </font> spinner = <font color="#0000AA">new</font> JSpinner(<font color="#0000AA">new</font> SpinnerDateModel());
<font color="#555555">030 </font> cp.add(spinner);
<font color="#555555">031 </font> <font color="#00AA00">//Spinner für Wochentage</font>
<font color="#555555">032 </font> spinner = <font color="#0000AA">new</font> JSpinner(<font color="#0000AA">new</font> SpinnerListModel(WDAYS));
<font color="#555555">033 </font> cp.add(spinner);
<font color="#555555">034 </font> }
<font color="#555555">035 </font>
<font color="#555555">036 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">037 </font> {
<font color="#555555">038 </font> Listing3704 frame = <font color="#0000AA">new</font> Listing3704();
<font color="#555555">039 </font> frame.setLocation(100, 100);
<font color="#555555">040 </font> frame.setSize(300, 150);
<font color="#555555">041 </font> frame.setVisible(<font color="#006699">true</font>);
<font color="#555555">042 </font> }
<font color="#555555">043 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing3704.java"><font color="#000055" size=-1>Listing3704.java</font></a></td>
</tr>
</table>
<i>
Listing 37.4: Die Klasse JSpinner</i></p>
<p>
Die Ausgabe des Programms ist:
<p>
<a name="imageid037004"></a>
<img src="images/JSpinner1.gif">
<p>
<p><i>
Abbildung 37.4: Die Klasse JSpinner</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="k100237.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100237.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100239.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100243.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>
|