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

GridBagLayout sizes JScrollPane incorrectly

XMLWordPrintable

    • x86
    • windows_95



      Name: clC74495 Date: 06/18/98


      In JDK1.2 beta4 i&J on Windows NT,
      GridBagLayout doesn't respect the preferred height of a JScrollPane if
      the enclosing panel has a width less thant the preferred width of the JScrollPane.
      If the JScrollPane happens to contain a JTable, this sizing problem
      causes the JTable to throw exceptions.

      Run the test code below and follow the instructions in the comment.
      ---------
      import java.awt.*;
      import java.awt.event.*;
      import com.sun.java.swing.*;

      /**
       * BugTest is a little program to demonstrate the incorrect sizing
       * behavior of JScrollPane controls inside GridBagLayout in JDK1.2beta4i & 4j.
       * The preferred size of the scrollable component is 400, 200, however,
       * when using GridBagLayout the preferred height is ignored if the actual
       * width is less than the preferred width. To compound the problem, if
       * a JTable is being displayed it throws exceptions due to the incorrect
       * sizing.
       *
       * To see the sizing but:
       * o run "java BugTest" - JTextArea using GridBagLayout.
       * o size window so width gets smaller than initial width. Do
       * it several times. Height gets really small.
       * o size window bigger than initial width. Things return to normal.
       *
       * To see the JTable throw exceptions:
       * o run "java BugTest -table"
       * o size window so width gets smaller than initial width. Do
       * it several times if needed.
       *
       * To see what SHOULD happen, run test with BorderLayout instead of
       * GridBagLayout:
       * o run "java BugTest -border" or "java BugTest -border -table"
       *
       * Email me questions if needed at ###@###.###
       */
      public class BugTest
      {
          public static void main( String[] args )
          {
              boolean useBorderLayout = false;
              boolean useTable = false;
              for ( int i = 0; i < args.length; i++ )
              {
                  if ( args[i].equals( "-border" ) ) useBorderLayout = true;
                  else if ( args[i].equals( "-table" ) ) useTable = true;
              }

              JFrame frame = new JFrame( "size me smaller to see sizing bug" );
              JComponent scrollableComponent = null;

              if ( useTable )
              {
                  System.out.print("Using JTable");
                  // try it with JTable, it throws exceptions when sizing
                  scrollableComponent = new JTable(
                      new Object[][] {
                          { "1", "2", "3", "4", "5", "6" },
                          { "1", "2", "3", "4", "5", "6" },
                          { "1", "2", "3", "4", "5", "6" },
                          { "1", "2", "3", "4", "5", "6" },
                          { "1", "2", "3", "4", "5", "6" },
                          { "1", "2", "3", "4", "5", "6" },
                          { "1", "2", "3", "4", "5", "6" },
                          { "1", "2", "3", "4", "5", "6" },
                          { "1", "2", "3", "4", "5", "6" },
                          { "1", "2", "3", "4", "5", "6" },
                          { "1", "2", "3", "4", "5", "6" }
                      },
                      new Object[] { "1", "2", "3", "4", "5", "6" }
                  );
                  ((JTable)scrollableComponent).setPreferredScrollableViewportSize( new
      Dimension( 400, 200 ) );
              }
              else
              {
                  System.out.print("Using JTextArea");
                  // try it with JTextArea to just see sizing problem
                  scrollableComponent = new JTextArea( "Should be 200 pixels in height" ) {
                      public Dimension getPreferredScrollableViewportSize()
                      {
                          return new Dimension( 400, 200 );
                      }
                  };
              }

              if ( useBorderLayout )
              {
                  System.out.println(" in BorderLayout");
                  // border layout test
                  frame.getContentPane().setLayout( new BorderLayout() );
                  frame.getContentPane().add( new JScrollPane( scrollableComponent ),
      BorderLayout.NORTH );
              }
              else
              {
                  System.out.println(" in GridBagLayout");
                  // gridbag test
                  frame.getContentPane().setLayout( new GridBagLayout() );
                  GridBagConstraints gbc = new GridBagConstraints();
                  gbc.gridx = 0;
                  gbc.gridy = 0;
                  gbc.weightx = 1;
                  gbc.fill = gbc.HORIZONTAL;

                  frame.getContentPane().add( new JScrollPane( scrollableComponent ), gbc );

                  JPanel panel = new JPanel( new BorderLayout( 0, 5 ) );
                  gbc.gridy++;
                  gbc.weighty = 1;
                  gbc.fill = gbc.BOTH;
                  frame.getContentPane().add( panel, gbc );
              }

              // all tests
              frame.pack();
              frame.setVisible( true );
          }
      }
      (Review ID: 33835)
      ======================================================================

            tdv Dmitri Trembovetski (Inactive)
            clucasius Carlos Lucasius (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: