Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to size a JTextField according to a fixed string length?

23 views
Skip to first unread message

Casper Bang

unread,
Oct 28, 2005, 11:31:50 AM10/28/05
to
How do I size a JTextField, such that I know for a fact that it can
display a certain string lenght? I do not want the field to grow or
shrink according to content, it should be fixed upon initial creation.

Thanks in advanced,
Casper

Oliver Wong

unread,
Oct 28, 2005, 11:56:17 AM10/28/05
to

"Casper Bang" <cas...@jbr.dk> wrote in message
news:43624468$0$38734$edfa...@dread12.news.tele.dk...

If it is not mission critical that the string can be displayed, use the
setColumns method. It assumes that in the font being used, 'm' us the widest
character, and sizes the JTextField to be able to fit the string composed of
N 'm's appended together.

If it IS mission critical, you will need to get a hold of the Font
object, and render every character into an off-screen buffer, measure their
widths, and the use the maximal width as the width of a single column,
multiply it by how long you string is, set the width of the JTextArea to
that. There are approximately one million Unicode characters.

- Oliver


0 new messages