aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes TextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "TextArea" Watch "TextArea" New topic
Author

TextArea

josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216
Check out my answers
A TextArea is made in a proportional font like this:
TextArea ta = new TextArea ("hello", 5, 5);
Which are true?
a) It will show exactly 5 rows.
b) It will show exactly 5 columns.
c) Each item can only be 5 letters long.
d) You can edit the contents.
e) You can have multiple fonts inside it.
a,b,c
Siddhu
Ranch Hand

Joined: Oct 02, 1999
Posts: 31
I think the answers should be a) b) d)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I think it's just A and D. In a proportional font, the widths of the characters vary, and you can fit a lot more "1"'s in a given space than you can "W"'s. The width of the TextArea will be 5 "average" characters, but you may see more or less at one time, and if you keep typing on a line the TextArea will scroll horizontally to allow you to keep typing. So B and C are both wrong.
I can't find a spec that says the TextField will be editable by default, but it seems to be true for jdk 1.3 beta at least.


"I'm not back." - Bill Harding, Twister
Hugh Smith
Greenhorn

Joined: Feb 04, 2000
Posts: 6
In RHE p328 TextArea(String text, int nRows, int nCols) constructs a textarea whose initial content is text, with the specified number of rows and columns. So a & b are true. When you want it uneditable you have to call the setEditable(false) method so, it look like it defaults to editable, so d is true. C is definitely false. And given the examples given in RHE I would assume that only one font at a time, but I haven't tested that. Provisionally e false.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
No, B is definitely false because it contains the word "exactly". Note that RHE p.329 addresses the issue of proportional fonts, and states that the column width is taken as an average of all the characters' widths. So if characters have above-average width, not so many will fit in the field, while for below-average width you can fit more in. I have tried this with an actual TextArea to verify it - "5 character widths" was enough for almost 3 "W"'s, or about ten "1"'s, using my default font.
As for multiple fonts - what method could you use to have different fonts? I don't think there's anything to test, unless someone can suggest a method to try. TextArea has only one setFont() method, and it sets the font for the whole area.
Hugh Smith
Greenhorn

Joined: Feb 04, 2000
Posts: 6
I think that there is a miscommunication between characters and columns. The API says:
TextArea
public TextArea(String text,
int rows,
int columns)
Constructs a new text area with the specified text, and with the specified number of rows and columns. This text area is created with both vertical and horizontal scroll bars.
Parameters:
text - the text to be displayed.
rows - the number of rows.
columns - the number of columns.
------------------------------------------------------------
So, the matter of how many characters will fit into a column is a different question than how many columns, I think.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
But the idea of "columns" makes no sense if you're using proportional fonts - the characters don't line up into columns.
This is in a proportional font:
12345678901234567890123456
abcdefghijklmnopqrstuvwxyz
WWWlllllllllllllllllWWWWWW
lllllllWWWWWWWWWWWWlllllll
12345678901234567890123456
And this is in a constant-width font:
<code>12345678901234567890123456
abcdefghijklmnopqrstuvwxyz
WWWlllllllllllllllllWWWWWW
lllllllWWWWWWWWWWWWlllllll
12345678901234567890123456</code>
See the difference? How wide should a TextArea be to enclose 5 "columns" in the first case?
Since the API says nothing at all about what "columns" means when using a proportional font, ignore it. Since RHE do address the issue, listen to them. And since you should have access to a working jdk, try this:


[This message has been edited by Jim Yingst (edited February 10, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I think ONLY d is Correct.
The TextArea constructed with TextArea("Somestring", 5,5) will not EXACTLY display 5 rows, so A is also false
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Heh. I didn't even notice that - you're right. At least using JDK 1.3 on Win 98. It sure looks like there's space for 5 rows, but when you put the fifth one in it does a little auto-scroll for no good reason, and you only see 4 lines plus a blank area that's probably big enough fo 95% of a line. How annoying.
I'm inclined to call this a bug, however. The TextArea certainly should be able to display 5 rows fully, since even with proportional fonts the characters are always in discrete rows, though not in discrete columns. I.e. while it's fundamentally impossible for B to be true, A is only false because someone screwed up. I've noticed similar minor sizing bugs elsewhere in AWT and Swing - e.g. scrollbars that don't appear quite as readily as they need to when you resize a ScrollPane. You can sometimes get up to half a line out of view without the scrollbar appearing. Very annoying.
As a last note, if you replace TextArea with a JTextArea, it works fine, displaying exactly 5 rows (at least on my platform).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: TextArea