JDK-4339681 : JTextField.getPreferredSize() returns a size that is 5 pixels too narrow.
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2000-05-19
  • Updated: 2001-09-20
  • Resolved: 2001-09-20
Related Reports
Duplicate :  
Description

Name: rlT66838			Date: 05/19/2000


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

Compile and run the program shown below. It creates a window with a 20-character
JTextField. The field is initially set to use Courier-18; you can switch back
and forth between Courier-18 and Courier-36 by pressing Return.
Notice how, at both font sizes, the field is a bit too narrow for 20 characters
of text. If you move the caret to just before the first character, and then move
it to just after the last character, the field scrolls by 5 pixels.
This is independent of the font size or the platform. I have confirmed this on
Solaris 2.6 running JDK 1.2.2, and on Windows NT 4.0 sp5 running JDK 1.2.2 and
1.3.

public class SizeTest implements ActionListener {

    public static void main(String[] args) {
        new SizeTest(args);
    }

    private JFrame frame;
    private JTextField textfield;
    private boolean small = true;

    private SizeTest(String[] args) {
        frame = new JFrame();
        Container cpanel = frame.getContentPane();
        cpanel.setLayout(new GridLayout(1, 1));
        textfield = new JTextField(20);
        textfield.setFont(new Font("Courier", Font.PLAIN, 18));
        textfield.addActionListener(this);
        cpanel.add(textfield);
        frame.pack();
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        small = !small;
        textfield.setFont(new Font("Courier", Font.PLAIN, small ? 18 : 36));
        textfield.invalidate();
        frame.pack();
    }
}
(Review ID: 105130) 
======================================================================

Comments
EVALUATION Name: keR10081 Date: 09/20/2001 This bug is the same with 4426701, but since the latter has better information, I close this bug as a duplicate. ###@###.### ======================================================================
11-06-2004

WORK AROUND Name: rlT66838 Date: 05/19/2000 Override JTextField.getPreferredSize() and add 5 to the width returned by super.getPreferredSize(). ======================================================================
11-06-2004