• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextfield getColumns() method

 
Tal Goldstein
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
im trying to get information from a textfield for how many columns in the text field are currently visible.
for example, if i open a certain window which contains a text field,
so i would like to know, how many chars can be written to the textfield until not all of them can be seen,
and of course when i enlarge that window, then the number chars i can write will be bigger..

i tried using getColumns() but it always returns 0.

any ideas?

thanks
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's probably because you created it without setting the number of columns. However your question is going in the wrong direction because the number of columns isn't going to tell you how many characters can be seen in the field at its current width. For one thing, in most fonts characters have different widths, so you might be able to see 12 "w" characters or 26 "l" characters.
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tal Goldstein:
hi
im trying to get information from a textfield for how many columns in the text field are currently visible.
for example, if i open a certain window which contains a text field,
so i would like to know, how many chars can be written to the textfield until not all of them can be seen,
and of course when i enlarge that window, then the number chars i can write will be bigger..

i tried using getColumns() but it always returns 0.

any ideas?


The default for getColumns() is zero, but you can set it otherwise. The
value affects only the field's preferred size, not its actual size.
(And, of course, some layout managers ignore preferred size.) If zero,
the field will return a preferred size big enough for its text. If non-
zero it will return a preferred size approximately big enough for that
many chars, modulo Mr. Clapham's observation about variable-width fonts.

In any case, getColumns() doesn't do what you want.

One thing you could try is

but this will only report how many actual characters are displayed,
not how many characters could be displayed. If you have a wide
field with only three chars in it, visibleWidth will be 3.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!