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

Error in GridBagLayout code in the ArrangeGrid's function

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.4.2
    • client-libs
    • x86
    • windows_xp



      Name: rmT116609 Date: 03/02/2004


      FULL PRODUCT VERSION :
      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)

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

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :

      In the GridBagLayout.java, in the function:

      protected void ArrangeGrid(Container parent)

      There's the following lines:

      /* fix for 4408108 - components were being created outside of the container */
            if (r.x < 0) {
                r.width -= r.x;
                r.x = 0;
            }
                
            if (r.y < 0) {
               r.height -= r.y;
               r.y = 0;
            }

      My interpretation is that when r.x is negative, then make it 0, in effect also decrease the size of the component. To achive this, the code should look like:

            if (r.x < 0) {
                r.width += r.x;
                r.x = 0;
            }
                
            if (r.y < 0) {
               r.height +-= r.y;
               r.y = 0;
            }

      In another word, it has to be +, not -, because - of a negative value actually increase the size. I see this in a real app. When decreasing a window to make it smaller, some component start to grow in size versus decrease in size.


      Test Case:

      import javax.swing.*;
      import java.awt.*;

      public class TestGridBag extends javax.swing.JFrame {
         
          /** Creates new form TestGridBag */
          public TestGridBag() {
              setWindowsLF();
              initComponents();
          }
          public void setWindowsLF(){
              String lnf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
              try {
                  UIManager.setLookAndFeel(lnf);
              } catch(Exception e) {
                  e.printStackTrace();
              }
              SwingUtilities.updateComponentTreeUI(this);
          }
          /** This method is called from within the constructor to
           * initialize the form.
           * WARNING: Do NOT modify this code. The content of this method is
           * always regenerated by the Form Editor.
           */
          private void initComponents() {
              java.awt.GridBagConstraints gridBagConstraints;

              jLabel1 = new javax.swing.JLabel();
              jButton1 = new javax.swing.JButton();
              jButton2 = new javax.swing.JButton();
              jButton3 = new javax.swing.JButton();
              jPanel1 = new javax.swing.JPanel();

              FormListener formListener = new FormListener();

              getContentPane().setLayout(new java.awt.GridBagLayout());

              addWindowListener(formListener);

              jLabel1.setText("jLabel1");
              jLabel1.setMinimumSize(new java.awt.Dimension(0, 15));
              jLabel1.setPreferredSize(new java.awt.Dimension(0, 15));
              gridBagConstraints = new java.awt.GridBagConstraints();
              gridBagConstraints.gridx = 0;
              gridBagConstraints.gridy = 0;
              gridBagConstraints.gridheight = 2;
              gridBagConstraints.ipadx = 120;
              gridBagConstraints.ipady = 10;
              gridBagConstraints.insets = new java.awt.Insets(114, 0, 0, 0);
              getContentPane().add(jLabel1, gridBagConstraints);

              jButton1.setText("jButton1");
              jButton1.setMinimumSize(new java.awt.Dimension(72, 23));
              jButton1.setPreferredSize(new java.awt.Dimension(72, 23));
              gridBagConstraints = new java.awt.GridBagConstraints();
              gridBagConstraints.gridx = 1;
              gridBagConstraints.gridy = 0;
              gridBagConstraints.gridwidth = 5;
              gridBagConstraints.gridheight = 3;
              gridBagConstraints.ipadx = 53;
              gridBagConstraints.ipady = 2;
              gridBagConstraints.insets = new java.awt.Insets(120, 90, 0, 0);
              getContentPane().add(jButton1, gridBagConstraints);

              jButton2.setText("jButton2");
              jButton2.setMargin(new java.awt.Insets(0, 0, 0, 0));
              jButton2.setMinimumSize(new java.awt.Dimension(72, 23));
              jButton2.setPreferredSize(new java.awt.Dimension(72, 23));
              gridBagConstraints = new java.awt.GridBagConstraints();
              gridBagConstraints.gridx = 3;
              gridBagConstraints.gridy = 3;
              gridBagConstraints.gridwidth = 5;
              gridBagConstraints.gridheight = 3;
              gridBagConstraints.ipadx = 38;
              gridBagConstraints.insets = new java.awt.Insets(89, 69, 0, 0);
              getContentPane().add(jButton2, gridBagConstraints);

              jButton3.setText("jButton3");
              jButton3.setMinimumSize(new java.awt.Dimension(72, 23));
              jButton3.setPreferredSize(new java.awt.Dimension(72, 23));
              gridBagConstraints = new java.awt.GridBagConstraints();
              gridBagConstraints.gridx = 3;
              gridBagConstraints.gridy = 6;
              gridBagConstraints.gridwidth = 4;
              gridBagConstraints.gridheight = 2;
              gridBagConstraints.ipadx = 48;
              gridBagConstraints.insets = new java.awt.Insets(18, 30, 0, 0);
              getContentPane().add(jButton3, gridBagConstraints);

              jPanel1.setBorder(new javax.swing.border.LineBorder(new
      java.awt.Color(0, 0, 0)));
              jPanel1.setMinimumSize(new java.awt.Dimension(13, 12));
              jPanel1.setPreferredSize(new java.awt.Dimension(13, 12));
              gridBagConstraints = new java.awt.GridBagConstraints();
              gridBagConstraints.gridx = 0;
              gridBagConstraints.gridy = 3;
              gridBagConstraints.gridwidth = 3;
              gridBagConstraints.gridheight = 6;
              gridBagConstraints.ipadx = 227;
              gridBagConstraints.ipady = 138;
              gridBagConstraints.insets = new java.awt.Insets(9, 0, 0, 0);
              getContentPane().add(jPanel1, gridBagConstraints);

              java.awt.Dimension screenSize =
      java.awt.Toolkit.getDefaultToolkit().getScreenSize();
              setBounds((screenSize.width-499)/2, (screenSize.height-394)/2,
      499, 394);
          }

          // Code for dispatching events from components to event handlers.

          private class FormListener implements java.awt.event.WindowListener {
              public void windowActivated(java.awt.event.WindowEvent evt) {
              }

              public void windowClosed(java.awt.event.WindowEvent evt) {
              }

              public void windowClosing(java.awt.event.WindowEvent evt) {
                  if (evt.getSource() == TestGridBag.this) {
                      TestGridBag.this.exitForm(evt);
                  }
              }

              public void windowDeactivated(java.awt.event.WindowEvent evt) {
              }

              public void windowDeiconified(java.awt.event.WindowEvent evt) {
              }

              public void windowIconified(java.awt.event.WindowEvent evt) {
              }

              public void windowOpened(java.awt.event.WindowEvent evt) {
              }
          }
         
          /** Exit the Application */
          private void exitForm(java.awt.event.WindowEvent evt) {
              System.exit(0);
          }
         
          /**
           * @param args the command line arguments
           */
          public static void main(String args[]) {
              new TestGridBag().show();
          }
         
         
          // Variables declaration - do not modify
          public javax.swing.JButton jButton1;
          public javax.swing.JButton jButton2;
          public javax.swing.JButton jButton3;
          public javax.swing.JLabel jLabel1;
          public javax.swing.JPanel jPanel1;
          // End of variables declaration
         
      }




      When you run the test case, when you slowly resizing the window to make it smaller, notice the left panel gets larger instead of getting smaller. It's natural to expect as windows size get smaller, its components should get smaller or the same size, but not grow larger.


      REPRODUCIBILITY :
      This bug can be reproduced always.
      (Incident Review ID: 241049)
      ======================================================================

            dav Andrei Dmitriev (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: