• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Swing : JTextField.setColumns(int) issue

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

i have a problem using the JTextField.setColumns(int) method: the
number of columns displayed is not the expected one. The result on
the display is always a bigger number of columns.
Do you know the reason of this strange behavior or a way to work
around this issue ?

Thanks.

Gilles
[ April 24, 2008: Message edited by: Gilles Marceau ]
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gilles Marceau:
i have a problem using the JTextField.setColumns(int) method: the
number of columns displayed is not the expected one. The result on
the display is always a bigger number of columns.
Do you know the reason of this strange behavior or a way to work
around this issue ?



The default font (in most LnFs) is a proportional-width font. Because
of this, setColumns() is not exact. Last time I checked, if you call
setColumns(6) and set the text to "mmmmmm" the field will actually
be a few pixels too short due to insets issues. [two notes: (1) 'm' is
usually a font's widest char. (2) I'm presuming the layout manager
lays out the JTextField at its preferred size.]

One way to fix a JTextField to be exactly the width of some text is
to do something like:
JTextField tf = new JTextField("some text");
tf.setPreferredSize(tf.getPreferredSize());
tf.setText("");
yourContainer.add(tf);

[ April 24, 2008: Message edited by: Brian Cole ]
 
Gilles Marceau
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for the advice, this is really helpful.

Gilles
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic