title: Java Swing Gridbaglayout look
body: <p>I've been working on a program that creates a dynamically sized table which depends on user input from another window. The code isnt directly related to my display using gridbaglayout so I wont post it. In my picture you can see that under the "SG" columns the "Comp #" and "System #", every new item adds in a position that the number of items in the column are given equal room between the items in the column. I want these items to just add and be directly under the title.</p>
  
  <p><a href="https://i.stack.imgur.com/E2jC0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E2jC0.png" alt="Pic"></a></p>
  
  <p>Here is source code of the important stuff. Wont be able to execute.</p>
  
  <pre><code>public JPanel panelCondenser(int numCond, int numSg) {
  
      // Condenser Panel will list the condensers 
      JLabel label;
      GridBagLayout gbl = new GridBagLayout();
      GridBagConstraints c = new GridBagConstraints();
      // Return a panel containing condenser labels
      JPanel panel = new JPanel(gbl);
      int numCols = numSg + 1;
      double numPerCol = Math.ceil((double) numCond / numCols);
      //System.out.println("Ceil of " + numCond + "/" + numCols + "=" + numPerCol);
  
      int numAdded = 0;
      //===========================
      // RACK CONDENSER
      //===========================
      //label = new JLabel("Condensers");
      //label.setFont(font);
      //label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      c.fill = GridBagConstraints.HORIZONTAL;
      //c.gridx = 0;
      c.weightx = 1;
      c.weighty = 1;
      //c.gridy = 0;
      //c.gridheight = 1;
      //panel.add(label, c);
  
      int colIndex = 0;
      int rowIndex = 2;
  
      for (int i = 1; i &lt;= numCond; i++) {
  
          if (numAdded &lt; numPerCol) {
              c.gridx = colIndex;
              c.gridy = rowIndex++;
              numAdded++;
          } else {
              numAdded = 1;
              colIndex++;
              rowIndex = 2;
              c.gridx = colIndex;
              c.gridy = rowIndex++;
          }
  
          label = new JLabel("Fan " + i);
          label.setFont(font);
          label.setBorder(border);
          c.fill = GridBagConstraints.HORIZONTAL;
          label.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
          panel.add(label, c);
      }
  
      panel.setBorder(border);
      return panel;
  }
  
  public JPanel panelCompressor(int numComp, int sgNum) {
  
      // Condenser Panel will list the condensers 
      JLabel label;
      GridBagLayout gbl = new GridBagLayout();
      GridBagConstraints c = new GridBagConstraints();
      // Return a panel containing condenser labels
      JPanel panel = new JPanel(gbl);
  
      //===========================
      // RACK COMPRESSOR
      //===========================
      label = new JLabel("SG " + sgNum);
      label.setBorder(border);
      label.setFont(font);
      label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      c.fill = GridBagConstraints.HORIZONTAL;
      c.anchor = GridBagConstraints.FIRST_LINE_START;
      c.gridx = 0;
      c.weightx = 1;
      c.weighty = 0;
      c.gridy = 0;
      c.gridheight = 1;
  
      panel.add(label, c);
      //c.fill = GridBagConstraints.BOTH;
      //==========================================================
      //                  Compressors
      //==========================================================
      for (int i = 1; i &lt;= numComp; i++) {
          label = new JLabel("Comp " + i + "      ");
          label.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
          c.fill = GridBagConstraints.HORIZONTAL;
          c.gridx = 0;
          c.gridy = i;
          //c.weighty = 1;
          label.setFont(font);
          label.setBorder(border);
          panel.add(label, c);
      }
  
      panel.setBorder(border);
      return panel;
  }
  
  public JPanel panelSystems(int numSystems, SuctionGroup sg, int sgNum) {
  
      // Condenser Panel will list the condensers 
      JLabel label;
      GridBagLayout gbl = new GridBagLayout();
      GridBagConstraints c = new GridBagConstraints();
      // Return a panel containing condenser labels
      JPanel panel = new JPanel(gbl);
      //==========================================================
      // SYSTEMS
      //==========================================================
      label = new JLabel("SG " + sgNum);
      label.setBorder(border);
      label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      c.fill = GridBagConstraints.HORIZONTAL;   
      c.anchor = GridBagConstraints.FIRST_LINE_START;
      c.gridx = 0;
      c.weightx = 1;
      c.weighty = 1;        
      c.gridy = 0;
      c.gridheight = 1;
  
      label.setFont(font);
      panel.add(label, c);
      //c.gridheight = 8;
      //c.fill = GridBagConstraints.BOTH;
  
      for (int i = 1; i &lt;= numSystems; i++) {
          label = new JLabel(sg.getSystemNameIndex((i - 1)) + "      ");
          label.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
          c.fill = GridBagConstraints.HORIZONTAL;
          //c.anchor = GridBagConstraints.LINE_START;
          c.gridx = 0;
          c.gridy = i;
          label.setFont(font);
          label.setBorder(border);
          panel.add(label, c);
      }
      panel.setBorder(border);
      return panel;
  }
  </code></pre>
  
  <p>Calling function</p>
  
  <pre><code>public void updateView() {
  
      // Vars used        
      int[] rackGridWidth = new int[5];
      int gridXPos, gridYPos, gridWidth, gridHeight;
      Rack r;
      SuctionGroup sg;
      JLabel label;
      JPanel panel = new JPanel();
      GridBagLayout gbl = new GridBagLayout();
      GridBagConstraints c = new GridBagConstraints();
  
      _Panel_MainPanel.setLayout(gbl);
      _Panel_MainPanel.removeAll();
  
      // First initial cell
      label = new JLabel("Outside Air Temp");
      label.setFont(font);
      panel.add(label);
      panel.setBorder(border);
      //===========================================================
      // Positioning
      gridXPos = 0;
      gridYPos = 0;
      gridWidth = 2;
      gridHeight = 5;
      // Constraints               
      c.fill = GridBagConstraints.BOTH;
      c.weightx = 1;
      c.weighty = 0; // No space between bottom and below row?        
      c.gridx = gridXPos;
      c.gridy = gridYPos;
      c.gridwidth = gridWidth;
      c.gridheight = gridHeight;
      //c.ipady = 100;
      //c.ipady = 0;        
      // Setup next position
      gridXPos += gridWidth;
      //gridYPos += gridHeight;
  
      // End of Constraints
      //===========================================================        
      _Panel_MainPanel.add(panel, c);
  
      // rack names + SEI, they have their own panels, incase we want borders
      for (int i = 0; i &lt; this.numRacks; i++) {
  
          r = racks[i];
          // Number of suction groups for the rack          
  
          panel = panelRackName(r.getName());
          // For each new rack panel, we must assign the grid width
          // to be 3 * num Suctiongroups
          rackGridWidth[i] = 3 * r.numSuctionGroups; // 1 cell per compressor
          // 2 cells per system
          //===========================================================            
          // Constraints        
          //c.fill = GridBagConstraints.BOTH;        
          //c.weightx = 1;
          //c.weighty = 0; // No space between bottom and below row?        
          c.gridx = gridXPos;
          c.gridy = gridYPos;
          c.gridwidth = rackGridWidth[i];
          //c.gridheight = gridHeight;
          //c.ipady = 100;
          //c.ipady = 0;                  
          // Setup next position
          gridXPos += rackGridWidth[i];
          // End of Constraints
          //===========================================================
          _Panel_MainPanel.add(panel, c);
  
      }
  
      //===========================
      // RACKS STATUS Condenser Fans
      //===========================
      //===========================================================
      // Positioning
      gridXPos = 0;
      gridYPos += gridHeight;
      gridWidth = 2;
      gridHeight = 10;
      // Constraints        
      c.fill = GridBagConstraints.BOTH;
      c.weightx = 1;
      c.weighty = 1; // No space between bottom and below row?        
      c.gridx = gridXPos;
      c.gridy = gridYPos;
      c.gridwidth = gridWidth;
      c.gridheight = gridHeight * 2;
      c.ipady = 0;
      //c.ipady = 0;        
      // Setup next position
      gridXPos += gridWidth;
      //gridYPos += gridHeight;
      // End of Constraints
      //=========================================================== 
  
      //===========================================================
      GridBagLayout gblStatus = new GridBagLayout();
      GridBagConstraints c1 = new GridBagConstraints();
      //===========================================================            
      // Constraints         for c2
      c1.fill = GridBagConstraints.BOTH;        
      c1.weightx = 1;
      c1.weighty = 0; // No space between bottom and below row?        
      c1.gridx = 0;
      c1.gridy = 0;
      c1.gridwidth = 2;
      c1.gridheight = 5; // 2 spots per row
      c1.ipady = 0;
      //c1.ipadx = 0;                  
      // We dont setup next position because we are adding suction groups still
      // End of Constraints
      //===========================================================
      // Rack status
      panel = new JPanel(gblStatus);
      label = new JLabel("Condenser Fans");
      label.setFont(font);
      label.setBorder(border);
      panel.add(label, c1);
      //===========================================================            
      // Constraints         for c1
      c1.fill = GridBagConstraints.HORIZONTAL;        
      //c1.weightx = 1;
      c1.weighty = 1; // No space between bottom and below row?        
      //c1.gridx = 0;
      c1.gridy = 5;
      //c1.gridwidth = 2;
      c1.gridheight = 14; // 2 spots per row
      c1.ipady = 0;
      //c1.ipadx = 0;                  
      // We dont setup next position because we are adding suction groups still
      // End of Constraints
      //===========================================================
      label = new JLabel("Suction Groups");
      label.setFont(font);
      label.setBorder(border);
      panel.add(label, c1);
  
      label = new JLabel("blank");
      label.setBorder(border);
      c1.gridy = 19;
      c1.gridheight = 1;
      //c1.ipady = 75;
      //panel.add(label, c1);
  
      panel.setBorder(border);
      _Panel_MainPanel.add(panel, c);        
  
      // Add condensers
      for (int i = 0; i &lt; this.numRacks; i++) {
          r = racks[i];
          // Number of suction groups for the rack
          //===========================
          // RACK CONDENSER
          //===========================
          panel = panelCondenser(r.getNumCondenserFans(), r.getNumSuctionGroups());
  
          //===========================================================            
          // Constraints        
          //c.fill = GridBagConstraints.BOTH;        
          //c.weightx = 1;
          c.weighty = 0; // No space between bottom and below row?        
          c.gridx = gridXPos;
          c.gridy = gridYPos;
          c.gridwidth = rackGridWidth[i];
          c.gridheight = gridHeight;            
          c.ipady = 0;                  
          // We dont setup next position because we are adding suction groups still
          //gridXPos += rackGridWidth[i];
          //gridYPos += gridHeight;
          // End of Constraints
          //===========================================================
          _Panel_MainPanel.add(panel, c);
  
          // Suction Groups - Compressors then Systems   
          for (int j = 0; j &lt; r.numSuctionGroups; j++) {
  
              sg = r.getSuctionGroupIndex(j);
  
              // Compressor
              panel = panelCompressor(sg.getNumCompressors(), (j + 1));
              //===========================================================            
              // Constraints        
              //c.fill = GridBagConstraints.BOTH;        
              //c.weightx = 1;
              c.weighty = 0; // No space between bottom and below row?        
              c.gridx = gridXPos;
              c.gridy = gridYPos + gridHeight;
              c.gridwidth = 1;
              //c.gridheight = gridHeight;
              //c.ipady = 100;
              //c.ipady = 0;                  
              // We dont setup next position because we are adding suction groups still
              gridXPos += 1;
              //gridYPos += gridHeight;
              // End of Constraints
              //===========================================================
  
              _Panel_MainPanel.add(panel, c);
              //c.gridx = gridXPos;
              //c.fill = GridBagConstraints.HORIZONTAL;
  
              //gridWidth = 1;
              //c.gridwidth = gridWidth;
              //gridXPos += gridWidth;
              // System
              panel = panelSystems(sg.getNumSystems(), sg, (j + 1));
              //===========================================================            
              // Constraints        
              //c.fill = GridBagConstraints.BOTH;        
              //c.weightx = 1;
              //c.weighty = 0; // No space between bottom and below row?        
              c.gridx = gridXPos;
              //c.gridy = gridYPos;
              c.gridwidth = 2;
              //c.gridheight = gridHeight;
              //c.ipady = 100;
              //c.ipady = 0;                  
              // We dont setup next position because we are adding suction groups still
              gridXPos += 2;
              //gridYPos += gridHeight;
              // End of Constraints
              //===========================================================
              _Panel_MainPanel.add(panel, c);
          }
      } .... more code after like validate/repaint
  </code></pre>