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

JComponent.getPreferredSize() exposes internal data

XMLWordPrintable

    • x86
    • windows_2000

      Name: jl125535 Date: 12/05/2002


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

      The code also appears in 1.4.1.

      FULL OPERATING SYSTEM VERSION :
      Microsoft Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      When one modifies the Dimension-object returned
      by "getPreferredSize()"(or "getMinimumSize()") of a visible
      JPanel, the next call of "getPreferredSize()"
      (or "getMinimumSize()") of this JPanel returns the modified
      Dimension-object.

      The getPreferredSize() implementation that returns a handle to internal data
      is in javax.swing.JComponent.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create an instance of the Class JPanel.
      2. Show this JPanel in a frame.
      2. Call "getPreferredSize()" on this JPanel and save the
      result in an Dimension object.
      3. Increase the width of the Dimension object by 100.
      4. Call again "getPreferredSize()" on the JPanel.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      The first call of "getPreferredSize()" returns the
      following Dimension object : java.awt.Dimension
      [width=10,height=10].

      The second call of "getPreferredSize()" returns a Dimension
      object with by 100 increased width : java.awt.Dimension
      [width=110,height=10].


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class PreferredSizeBug extends JPanel {

      JComponent comp;

      public PreferredSizeBug() {
      comp = new JPanel();
      add(comp);
      }

      public void printSize() {
      Dimension minimum = comp.getMinimumSize();
      System.out.println("minimumSize : " + minimum);
      minimum.width += 100;
      minimum = comp.getMinimumSize();
      System.out.println("minimumSize : " + minimum);

      Dimension preferred = comp.getPreferredSize();
      System.out.println("preferredSize : " + preferred);
      preferred.width += 100;
      preferred = comp.getPreferredSize();
      System.out.println("preferredSize : " + preferred);
      }
      public static void main(String[] args) {
      PreferredSizeBug preferredSizeBug1 = new PreferredSizeBug();
      JFrame frame = new JFrame();
      frame.setContentPane(preferredSizeBug1);
      frame.pack();
      frame.setVisible(true);
      preferredSizeBug1.printSize();
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      Create an own JPanel and overwrite the
      methods "getMinimumSize()" and "getPreferredSize()" to
      return always a new Dimension object.
      (Review ID: 158562)
      ======================================================================

            son Oleg Sukhodolsky (Inactive)
            jleesunw Jon Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: