11package swingjs .a2s ;
22
3+ import java .awt .Color ;
4+
35import javax .swing .JScrollPane ;
46import javax .swing .JTextArea ;
57
@@ -35,35 +37,32 @@ public class TextArea extends JScrollPane {
3537
3638
3739 public TextArea (int rows , int cols ) {
38- super ();
39- setViewportView (ta = new JTextArea (rows , cols ));
40- awtDefaults ();
40+ this (null , rows , cols , SCROLLBARS_BOTH );
4141 }
4242
4343 public TextArea () {
44- super ();
45- setViewportView (ta = new JTextArea ());
46- awtDefaults ();
44+ this (null , 0 , 0 , SCROLLBARS_BOTH );
4745 }
4846
4947 public TextArea (String text ) {
50- super ();
51- setViewportView (ta = new JTextArea (text , 0 , 9 ));
52- awtDefaults ();
48+ this (text , 0 , 0 , SCROLLBARS_BOTH );
5349 }
5450
5551 public TextArea (String text , int rows , int cols ) {
56- super ();
57- setViewportView (ta = new JTextArea (text , rows , cols ));
58- awtDefaults ();
52+ this (text , rows , cols , SCROLLBARS_BOTH );
5953 }
6054
6155 public TextArea (String text , int rows , int columns , int scrollbars ) {
62- this (text , rows , columns );
56+ super ();
57+ if (rows < 0 )
58+ rows = 0 ;
59+ if (columns < 0 )
60+ columns = 0 ;
61+ setViewportView (ta = new JTextArea (text , rows , columns ));
6362 switch (scrollbars ) {
6463 case SCROLLBARS_BOTH :
65- setVerticalScrollBarPolicy (VERTICAL_SCROLLBAR_NEVER );
66- setHorizontalScrollBarPolicy (HORIZONTAL_SCROLLBAR_NEVER );
64+ setVerticalScrollBarPolicy (VERTICAL_SCROLLBAR_ALWAYS );
65+ setHorizontalScrollBarPolicy (HORIZONTAL_SCROLLBAR_ALWAYS );
6766 break ;
6867 case SCROLLBARS_VERTICAL_ONLY :
6968 setVerticalScrollBarPolicy (VERTICAL_SCROLLBAR_ALWAYS );
@@ -80,6 +79,20 @@ public TextArea(String text, int rows, int columns, int scrollbars) {
8079 }
8180 }
8281
82+ public int getScrollbarVisibility () {
83+ int vp = getVerticalScrollBarPolicy ();
84+ int hp = getHorizontalScrollBarPolicy ();
85+ if (vp == VERTICAL_SCROLLBAR_ALWAYS && hp == HORIZONTAL_SCROLLBAR_ALWAYS )
86+ return SCROLLBARS_BOTH ;
87+ if (vp == VERTICAL_SCROLLBAR_ALWAYS )
88+ return SCROLLBARS_VERTICAL_ONLY ;
89+ if (hp == HORIZONTAL_SCROLLBAR_ALWAYS )
90+ return SCROLLBARS_HORIZONTAL_ONLY ;
91+ return SCROLLBARS_NONE ;
92+
93+ }
94+
95+
8396 public void setCaretPosition (int pos ) {
8497 ta .setCaretPosition (pos );
8598 }
@@ -148,5 +161,17 @@ public int getColumns() {
148161 public int getRows () {
149162 return ta .getRows ();
150163 }
164+
165+ @ Override
166+ public void setBackground (Color c ) {
167+ if (ta != null )
168+ ta .setBackground (c );
169+ }
151170
171+ @ Override
172+ public void setForeground (Color c ) {
173+ if (ta != null )
174+ ta .setForeground (c );
175+ }
176+
152177}
0 commit comments