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

Preferred Size Ignores TitledBorder's Text Width

    Details

    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_2000, windows_xp

      Description

      Name: rmT116609 Date: 02/12/2004


      FULL PRODUCT VERSION :
      java version "1.4.2_03"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_03-b02)
      Java HotSpot(TM) Client VM (build 1.4.2_03-b02, mixed mode)

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

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      When a JComponent is given a TitledBorder, the JComponent's preferred size correctly adjusts its height to reflect the TitledBorder but not its width. The preferred width is only increased by 10 (5 pixels on either side) regardless of the width of the text of the TitledBorder.

      I have an example program below to illustrate it but here's a more concrete explanation:

      I have a JLabel without a border. Its preferred size is W=28 H=16. When I add a TitledBorder the preferred height is correctly increaesed to 42 but the width is only increased to 38, REGARDLESS OF THE TEXT IN THE TitledBorder.

      The result, of course, is that the text of the TitledBorder is promptly truncated and it looks like heck.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See "Source code" section below.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I would expect that the preferred size of the component takes into account the required width of the TitledBorder.
      ACTUAL -
      The text of the TitledBorder is truncated.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.Color;
      import java.awt.FlowLayout;

      import javax.swing.BorderFactory;
      import javax.swing.JFrame;
      import javax.swing.JLabel;


      public class Tester2 {
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.getContentPane().setLayout(new FlowLayout());
            
            JLabel label_1 = new JLabel("Hello");
            label_1.setBackground(Color.PINK);
            label_1.setOpaque(true);
            System.out.println("Before border: " + label_1.getPreferredSize());
            label_1.setBorder(BorderFactory.createTitledBorder("Rather Long TitledBorder Text"));
            System.out.println("After border: " + label_1.getPreferredSize());
            
            f.getContentPane().add(label_1);
            
            f.pack();
            f.setVisible(true);
        }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use TitledBorder.getMinimumSize(Component c) to manually (and painfully) change the preferred size of teh component. This is something the component shoudl do automatically.
      (Incident Review ID: 235891)
      ======================================================================

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                rmandalasunw Ranjith Mandala (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Imported:
                  Indexed: