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 ]