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

JTextArea.getPreferredSize() only works with Monospaced Font

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.2.0
    • 1.0.2, 1.2.0
    • client-libs
    • None
    • swing1.1
    • generic, x86, sparc
    • generic, solaris_2.6, windows_nt

      Example. For the text "Lights Tool" the metal JTextArea gives a
      preferredSize of "126 x 21" and on Motif (which looks correct
      it is "90 x 21".

      ---------------------------------------------
      I was initally fooled by this description, so I'm going to add more here.

      Here is an ASCII representation of the problem

      Motif:

      ---------------
      | Lights Tool |
      ---------------

      Metal:

      -----------------------------
      | Lights Tool |
      -----------------------------

      This is with Swing-1.0.2. The problem however is not limited to Metal or Motif. It happens that Motif uses Monospaced as it's font, but Metal uses SansSerif. I experimented with other fonts and it seems to only get the right answer with Monospaced. Dialog font returns strange answers as well.

      Here is a sample program I wrote to demonstrate the problem.


      import com.sun.java.swing.*;
      import com.sun.java.swing.border.*;

      import java.awt.*;

      class Scratch {

         public static void main(String[] args) {
            try {
               UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
               System.out.println("set motif");
            } catch (Exception e) {

               System.out.println(e);
            }
            JTextArea area = new JTextArea();
            area.setBorder(new LineBorder(Color.black));
            area.setText("Lights Tool");
            area.setFont(new Font("Dialog", Font.PLAIN, 12));
            System.out.println(area.getPreferredSize());
            System.out.println(area.getFont());

            JFrame f = new JFrame();
            f.getContentPane().add(area);
            f.getContentPane().setLayout(new FlowLayout());
            f.pack();
            f.show();
         }

      }

      When you run it, you will see that the TextArea stays about 2x as wide as it needs to be.


      steve.wilson@eng 1998-05-20

            tprinzing Tim Prinzing (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: