title: Why these three panels using GridBagLayout use extra space differently, and how to make them uniform
body: <p>I have an application that has multiple panels; I would like to have the freedom to use different layout managers for the different panels, but would like them to behave similarly as the window is resized by the user.</p>
  
  <pre><code>    package example;
  
      import java.awt.GridBagConstraints;
      import java.awt.GridBagLayout;
  
      import javax.swing.BoxLayout;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTabbedPane;
      import javax.swing.JTextField;
  
      public class TP1 extends JFrame
      {
          public static void main(String[] args)
          {
              TP1 tp1 = new TP1();
              tp1.go();
          }
  
          public void go()
          {
              setDefaultCloseOperation(EXIT_ON_CLOSE);
  
              // create a panel with some labels on it
              JPanel innerFirst = new JPanel();
              innerFirst.setLayout(new BoxLayout(innerFirst, BoxLayout.PAGE_AXIS));
              innerFirst.add(new JLabel("one"));
              innerFirst.add(new JLabel("two"));
              innerFirst.add(new JLabel("three"));
              innerFirst.add(new JLabel("four"));
  
              // put that panel in a scroll pane
              JScrollPane firstSP = new JScrollPane(innerFirst);
  
              // make another panel and put our scrolled panel in it
              JPanel outerFirst = new JPanel(); 
              outerFirst.setLayout(new BoxLayout(outerFirst, BoxLayout.PAGE_AXIS));
              outerFirst.add(firstSP); 
  
              // create a GridBagLayout panel with some text fields on it
              JPanel innerSecond = new JPanel(new GridBagLayout());
              GridBagConstraints gbc = new GridBagConstraints();
              gbc.gridx = 0;
              gbc.gridy = 0;
              gbc.weightx = .25;
              gbc.anchor = GridBagConstraints.LINE_START;
              innerSecond.add(new JTextField(8), gbc);
              gbc.gridx = 0;
              gbc.gridy = 1;
              innerSecond.add(new JTextField(10), gbc);
              gbc.gridx =0;
              gbc.gridy = 2;
              innerSecond.add(new JTextField(12), gbc);
  
              // put that panel in a scroll pane
              JScrollPane secondSP = new JScrollPane(innerSecond);
  
              // make another panel and put our second scrolled panel in it
              JPanel outerSecond = new JPanel(); 
              outerSecond.setLayout(new BoxLayout(outerSecond, BoxLayout.LINE_AXIS));
              outerSecond.add(secondSP); 
  
              JPanel innerThird = new JPanel(new GridBagLayout());
              GridBagConstraints gbc3 = new GridBagConstraints();
              gbc3.anchor = GridBagConstraints.LINE_END;
              gbc.weightx = .25;
              gbc3.gridx = 0;
              gbc3.gridy = 0;
              innerThird.add(new JLabel("1st label"), gbc3);
              gbc3.gridy = 1;
              innerThird.add(new JLabel("second label"), gbc3);
              gbc3.gridy = 2;
              innerThird.add(new JLabel("IIIrd label"), gbc3);
  
              gbc3.anchor = GridBagConstraints.LINE_START;
              gbc3.gridx = 1;
              gbc3.gridy = 0;
              innerThird.add(new JTextField(8), gbc3);
              gbc3.gridy = 1;
              innerThird.add(new JTextField(12), gbc3);
              gbc3.gridy = 2;
              innerThird.add(new JTextField(14), gbc3);
  
              JScrollPane thirdSP = new JScrollPane(innerThird);
              JPanel outerThird = new JPanel();
              outerThird.setLayout(new BoxLayout(outerThird, BoxLayout.LINE_AXIS));
              outerThird.add(thirdSP);
  
              // put the scrolled panes onto a tabbed pane
              JTabbedPane tp = new JTabbedPane();
              tp.add("text fields", outerSecond);
              tp.add("labels", outerFirst);
              tp.add("mixed", outerThird);
  
              // add the tabbed pane to the frame
              this.add(tp);
  
              // pack it and ship it.
              pack();
              setVisible(true);
          }
      }
  </code></pre>
  
  <p>Running the above code, we get a window with a tabbed pane in it with three tabs.  If we make the window smaller, all of them get scroll bars, as intended.  If we make it larger, the three behave differently: the tab with labels only leaves them at the top left of the window, the tab with fields only centers them vertically on the left edge, and the one with the gridbag of mixed labels and fields centers them both horizontally and vertically in the enlarged window.</p>
  
  <p>This is not acceptable for the application; I need to somehow make all the panels behave similarly in this way.  I need them all to have scroll bars, and I would like them all to keep to the upper left if the window is made larger than the internal panel.</p>
  
  <p>One other requirement: my tabs are occupied by something that extends JPanel, I've been told before I can put JScrollPane directly into the tab, but for my application I don't want to do that either.  It just makes other things more complicated than they need to be.</p>
  
  <p>In addition to wanting all the extra space to be put at the bottom and the right, I would dearly love to understand WHY these three situations behave differently.  I still believe that we would all be better off if we understood the principles behind what we're doing, instead of just copying examples right and left and doing things by trial and error until they work.</p>
  
  <p>(Incidentally, I have a GroupLayout panel that does seem to gravitate to the upper left, but didn't think it was necessary for my question and this is 100 lines of code as it is.)</p>