Source code: rcsdesign/AutoSizedTextArea.java
1 package rcsdesign;
2
3 import java.awt.*;
4
5
6 public class AutoSizedTextArea extends TextArea
7 {
8
9 public static boolean debug_on = false;
10
11 AutoSizedTextArea(String txt, int width, int height, int maxcols, int maxrows)
12 {
13 super(txt);
14 Frame tempFrame = new Frame();
15 tempFrame.add(this);
16 tempFrame.pack();
17 if(debug_on)
18 {
19 System.out.println("AutoSizedTextArea(String txt="+txt+", int width="+width+", int height="+height+")");
20 }
21 int cols = width/5;
22 int rows = height/5;
23 if(cols > maxcols)
24 {
25 cols = maxcols;
26 }
27 if(rows > maxrows)
28 {
29 rows = maxrows;
30 }
31 Dimension dm = getMinimumSize(rows,cols);
32 Dimension dp = getPreferredSize(rows,cols);
33 if(debug_on)
34 {
35 System.out.println("cols="+cols+", rows="+rows+", dp="+dp+", dm="+dm);
36 }
37 while((dm.width > width || dp.width > width) && cols > 10)
38 {
39 int bad_width = dp.width;
40 if(dm.width > dp.width)
41 {
42 bad_width = dm.width;
43 }
44 int diff = (bad_width - width)/20;
45 if(diff < 1)
46 {
47 diff = 1;
48 }
49 cols -= diff;
50 dm = getMinimumSize(rows,cols);
51 dp = getPreferredSize(rows,cols);
52 if(debug_on)
53 {
54 System.out.println("cols="+cols+", rows="+rows+", dp="+dp+", dm="+dm);
55 }
56 }
57 if(debug_on)
58 {
59 System.out.println("AutoSizedTextArea(): setColumns("+cols+");");
60 }
61 setColumns(cols);
62 while((dm.height > height || dp.height > height) && rows > 2)
63 {
64 int bad_height = dp.height;
65 if(dm.height > dp.height)
66 {
67 bad_height = dm.height;
68 }
69 int diff = (bad_height - height)/20;
70 if(diff < 1)
71 {
72 diff = 1;
73 }
74 rows -= diff;
75 dm = getMinimumSize(rows,cols);
76 dp = getPreferredSize(rows,cols);
77 if(debug_on)
78 {
79 System.out.println("cols="+cols+", rows="+rows+", dp="+dp+", dm="+dm);
80 }
81 }
82 if(debug_on)
83 {
84 System.out.println("AutoSizedTextArea(): setRows("+rows+");");
85 }
86 setRows(rows);
87 if(debug_on)
88 {
89 System.out.println("cols="+cols+", rows="+rows+", dp="+dp+", dm="+dm);
90 //System.out.println("AutoSizedTextArea(): tempFrame.dispose();");
91 }
92 //tempFrame.dispose();
93 if(debug_on)
94 {
95 System.out.println("AutoSizedTextArea() constructor complete.\n");
96 }
97 }
98 }
99
100
101
102
103