Reputation: 108
Per the JTextField
doc, you can set the width of the field delineated in “columns”, but cannot find a definition for “columns”.
Moreover, doing something like this:
new JTextField("abc", 3);
Results in a text field of width populated with the string abc
and then extra whitespace afterwards.
Can someone clarify the the definition of columns as used in the JTextField
constructor?
Upvotes: 3
Views: 7804
Reputation: 316
Thanks for the Enwired's answer. This bugged me for some time. To further elaborate Enwired's answer, it is the number of m's that the JTextField can display on the screen (panel or window).
Observations
new JTextField("mmm", 3); // this 3 is number of 'm' that we allow to fit in, please note that 'mmm' and 'abc' widths are different even though they are same character count, that is 3
For the above code it will display a JTextField with 3 m's width. (we can type more m's on this JTextField, but on the TextField it shows only 3m's)
If we type "abc" on this, there is still remaining space on the text field where we can accommodate another character like shown below,
new JTextField("mmm", 3);
so, the conclusion seems to be it (argument value 3) is not the number of characters that we can fit into JTextField, but the number of 'm' that we can fit in.
Upvotes: 0
Reputation: 1593
If you are using a standard JTextField
, the columns specifies the preferred width of the component as some multiple of getColumnWidth()
.
The method getColumnWidth()
defines the width in pixels of each column. The documentation states: "By default this is defined to be the width of the character 'm' for the font used."
The text field may display at a different size if the layout manager modifies it.
Upvotes: 4
Reputation: 29
The columns property of JTextField is used to determine the preferred width of the object. If an initial String is provided as in your example new JTextField("abc", 3);
then the field is painted to match the text size. Otherwise, the columns allow parallel stacking of text within the JTextField
itself. Therefore, the reason you get whitespace after the "abc"
in your example is because it defaults to place the string in the first column, then two columns of whitespace.
Upvotes: 1