File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes tricky textfields ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "tricky textfields !" Watch "tricky textfields !" New topic

tricky textfields !

sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
Here is the question folks:
When you create a textfield,
Textfield tf = new TextField(30);
what is the significane of 30, here.
I know that this is not the number of characters that are viewable.
Is that the maximum number of characters that can be entered in the field ?
I constructed a textfield like the above but was able to add infinite number of characters to it, the user just has to scroll using the arrow keys.
greg clarke

Joined: Oct 06, 2000
Posts: 25
The api doco states that TextField(int) constructs a new empty text field with the specified number of columns.
The number of columns is the number of characters that should be viewable, with the proviso that the actual size of the text field you see rendered depends on the font and layout manager.
And you can always scroll text fields - the number of columns has nothing to do with a maximum length.
The font may be fixed width or proportional, and the number of chars viewable will be affected accordingly. Also, the layout manager may disregard the preferred size of the text field.
However, if you use and fixed-width font and a layout manager that repects the preferred size of the text field, then you can see the relationship between number of columns and the actual size.
Try this code and you will see that the TextField with 10 columns is twice the length of the one with 20 columns. The fact that you don't fit exactly 10 or 20 chars in may be due to the way the length is calculated from the number of columns.
I agree. Here's the link:
subject: tricky textfields !
It's not a secret anymore!