Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4339681

JTextField.getPreferredSize() returns a size that is 5 pixels too narrow.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.3.0
    • client-libs



      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)
      ======================================================================

            tprinzing Tim Prinzing
            rlewis Roger Lewis (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: