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

GridBagLayout within JScrollPane has resizing problems

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0
    • client-libs
    • x86
    • windows_2000



      Name: sv35042 Date: 10/08/2002


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


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


      EXTRA RELEVANT SYSTEM CONFIGURATION :
      dual cpu

      A DESCRIPTION OF THE PROBLEM :
      I have observed strange interactions between a
      GridBagLayout within a JScrollPane. Whenever the
      vertical scrollbar appears on the scrollpane the top right
      component in the GrdBagLayout seems to be resize to
      the wrong dimensions.

      When the scrollbar is present the top right component
      in the GridBagLayout is made to be too large,
      and it obscures the second from the top component.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. run sample program
      2. resize frame so that vertical scrollbar appears
      3. notice that top-right component obscures second from top
      component. Also notice that labels on left overlap
      vertically.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      I expected that all components managed by the gridbag would
      be sized correctly.

      The actual results were that the components overlapped when
      the scrollbar was present. The amount of overlap changed as
      the frame size was shrunk.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Noneimport java.awt.*;
      import javax.swing.*;

      /**
       * This class exhibits strange interactions between a GridBagLayout
       * and a JScrollPane. Whenever the vertical scrollbar appears on
       * the scrollpane the top right component seems to be resize to the
       * wrong dimensions. To see this behaviour resize the frame so that
       * the vertical scrollbar just appears and then just disappears.
       *
       * When the scrollbar is present the top right component is made to
       * be too large, and it obscures the second from the top component.
       *
       * Removing the 'panel.setPreferredSize()' method call seems to
       * make the problem go away.

      Microsoft Windows 2000 [Version 5.00.2195]

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

      */
      public class Tester extends JFrame {

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


         public Tester() {
            super("GridBag in JScrollPane bug");

            getContentPane().setLayout(new BorderLayout());
            setBackground(Color.lightGray);
            //setBounds(100,100, 200, 200);

            String[] labels = {"One", "Two", "Three", "Four", "Five", "Six", "Seven",
      "Eight", "Nine"};
            Component[] components = {new CBox(Color.green),
      new JComboBox(new String[] {"Hello",
      "Goodbye"}),
      new CBox(Color.green),
      new JComboBox(new String[] {"Hello",
      "Goodbye"}),
      new JComboBox(new String[] {"Hello",
      "Goodbye"}),
      new CBox(Color.blue),
      new CBox(Color.red),
      new JComboBox(new String[] {"Hello",
      "Goodbye"}),
      new JComboBox(new String[] {"Hello",
      "Goodbye"}),
            };

            JPanel panel = new JPanel(new GridBagLayout());

            // Remove the following and the GridBagLayout works properly
            panel.setPreferredSize(new Dimension(200, 200));

            getContentPane().add(new JScrollPane(panel), BorderLayout.CENTER);


            for (int i=0; i<labels.length; i++) {

      panel.add(new JLabel(labels[i]), new GridBagConstraints(0, i,
      1, 1,
      1.0, 0.0,
      GridBagConstraints.WEST,
      GridBagConstraints.HORIZONTAL,
      new Insets(1,1,1,1),
      0, 0));

      panel.add(components[i], new GridBagConstraints(1, i,
      1, 1,
      1.0, 0.0,
      GridBagConstraints.EAST,
      GridBagConstraints.HORIZONTAL,
      new Insets(1,1,1,1),
      0, 0));
            }

            setVisible(true);

         }

         private class CBox extends JPanel {
            
            Color color;
            public CBox(Color color) {
      this.color = color;
      setMinimumSize(new Dimension(30,30));
            }
            
            public void paint(Graphics g) {
      g.setColor(color);
      g.fillRect(0, 0, getSize().width, getSize().height);

            }
         }
      }


      This bug can be reproduced always.

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

      /**
       * This class exhibits strange interactions between a GridBagLayout
       * and a JScrollPane. Whenever the vertical scrollbar appears on
       * the scrollpane the top right component seems to be resize to the
       * wrong dimensions. To see this behaviour resize the frame so that
       * the vertical scrollbar just appears and then just disappears.
       *
       * When the scrollbar is present the top right component is made to
       * be too large, and it obscures the second from the top component.
       *
       * Removing the 'panel.setPreferredSize()' method call seems to
       * make the problem go away.

      Microsoft Windows 2000 [Version 5.00.2195]

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

      */
      public class Tester extends JFrame {

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


         public Tester() {
            super("GridBag in JScrollPane bug");

            getContentPane().setLayout(new BorderLayout());
            setBackground(Color.lightGray);
            //setBounds(100,100, 200, 200);

            String[] labels = {"One", "Two", "Three", "Four", "Five", "Six", "Seven",
      "Eight", "Nine"};
            Component[] components = {new CBox(Color.green),
      new JComboBox(new String[] {"Hello",
      "Goodbye"}),
      new CBox(Color.green),
      new JComboBox(new String[] {"Hello",
      "Goodbye"}),
      new JComboBox(new String[] {"Hello",
      "Goodbye"}),
      new CBox(Color.blue),
      new CBox(Color.red),
      new JComboBox(new String[] {"Hello",
      "Goodbye"}),
      new JComboBox(new String[] {"Hello",
      "Goodbye"}),
            };

            JPanel panel = new JPanel(new GridBagLayout());

            // Remove the following and the GridBagLayout works properly
            panel.setPreferredSize(new Dimension(200, 200));

            getContentPane().add(new JScrollPane(panel), BorderLayout.CENTER);


            for (int i=0; i<labels.length; i++) {

      panel.add(new JLabel(labels[i]), new GridBagConstraints(0, i,
      1, 1,
      1.0, 0.0,
      GridBagConstraints.WEST,
      GridBagConstraints.HORIZONTAL,
      new Insets(1,1,1,1),
      0, 0));

      panel.add(components[i], new GridBagConstraints(1, i,
      1, 1,
      1.0, 0.0,
      GridBagConstraints.EAST,
      GridBagConstraints.HORIZONTAL,
      new Insets(1,1,1,1),
      0, 0));
            }

            setVisible(true);

         }

         private class CBox extends JPanel {
            
            Color color;
            public CBox(Color color) {
      this.color = color;
      setMinimumSize(new Dimension(30,30));
            }
            
            public void paint(Graphics g) {
      g.setColor(color);
      g.fillRect(0, 0, getSize().width, getSize().height);

            }
         }
      }

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

      CUSTOMER WORKAROUND :
      Not setting a preferred size on the interior panel seems to
      bypass the problem.
      (Review ID: 143886)
      ======================================================================

            dav Andrei Dmitriev (Inactive)
            svioletsunw Scott Violet (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: