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

JViewport: View-size caching can lead to incorrect scroll pane size

    XMLWordPrintable

Details

    Description

      FULL PRODUCT VERSION :
      D:\eclipse-SDK-2.1.1-win32\eclipse\workspace\Test>java -version
      java version "1.5.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
      Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)

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

      A DESCRIPTION OF THE PROBLEM :
      If a JScrollPane is contained in a layout which stretches the scroll pane horizontally, the result of JScrollPane.getPreferredSize() is incorrect (too high).
      This is caused by the scroll pane's JViewport, caching the view size.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the submitted source code which illustrates the bug

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The two scroll panes of the code example should have same size.
      ACTUAL -
      The scroll panes are different in height.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import javax.swing.*;

      /**
       * This class demonstrates how JViewport's view-size caching
       * can fail.
       */
      public class ViewportCachingTest extends JFrame {

      ViewportCachingTest() {
      super("ViewportCachingTest");
      setDefaultCloseOperation(EXIT_ON_CLOSE);

      getContentPane().setLayout(new GridBagLayout());
      GridBagConstraints gc = new GridBagConstraints();

      // strech all components horizontally
      gc.fill = GridBagConstraints.HORIZONTAL;
      gc.gridx = 0;
      gc.gridy = 1;

      // give the 1st cell an absolute width
      JLabel l = new JLabel();
      l.setOpaque(true);
      l.setBackground(Color.ORANGE);
      l.setPreferredSize(new Dimension(120, 8));
      getContentPane().add(l, gc);

      // this scroll pane's viewport is the default JViewport
      gc.gridy = 0;
      JScrollPane sp = new JScrollPane();
      sp.setViewportView(new JLabel(" Incorrect size"));
      getContentPane().add(sp, gc);

      // give the 2nd cell an absolute width
      gc.gridx = 1;
      gc.gridy = 1;
      l = new JLabel();
      l.setOpaque(true);
      l.setBackground(Color.GREEN);
      l.setPreferredSize(new Dimension(120, 8));
      getContentPane().add(l, gc);

      // this scroll pane's viewport is a NonCachingViewPort
      gc.gridy = 0;
      gc.fill = GridBagConstraints.HORIZONTAL;
      sp = new JScrollPane();
      sp.setViewport(new NonCachingViewPort());
      sp.setViewportView(new JLabel(" Correct size"));
      getContentPane().add(sp, gc);

      // give all cells an absolute height
      gc.gridx = 2;
      l = new JLabel();
      l.setOpaque(true);
      l.setBackground(Color.ORANGE);
      l.setPreferredSize(new Dimension(8, 60));
      getContentPane().add(l, gc);

      pack();
      setLocation(100, 100);
      setVisible(true);
      }

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

      /**
      * This class disables view-size caching by overriding
      * JViewPort.getViewSize().
      */
      class NonCachingViewPort extends JViewport {

      public Dimension getViewSize() {
      Component view = getView();

      if(view == null) {
      return new Dimension(0,0);
      }
      else {
      return view.getPreferredSize();
      }
      }
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Setting the scroll pane's viewport to a JViewport subclass which disables view-size caching.

      Attachments

        Activity

          People

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Imported:
              Indexed: