title: Layout Manager preferredSize Java
body: <p>I'm still trying to learn how layout managers work. I made a Frame with two JPanels.
  The first one contains a textArea with a boxLayout.
  The second one contains a flow layout with a button.</p>
  
  <p>I set the preferredSize of each panel accordingly, packed them, but got unexpected results.</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  
  public class LayoutMgrTest
  {
      public static void main(String[] args)
      {
          TableBasic frame = new TableBasic();
          frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
          frame.setVisible(true);
  
  
          frame.getContentPane().setLayout(new GridLayout(2,1));
  
          JPanel controlPane = new JPanel();
          JPanel buttonPane = new JPanel();
  
          controlPane.setLayout(new BoxLayout(controlPane, BoxLayout.PAGE_AXIS));
          controlPane.setPreferredSize(new Dimension(200, 200));
          controlPane.add(new JScrollPane(new JTextArea()));
  
          buttonPane.setLayout(new FlowLayout(FlowLayout.LEFT));
          buttonPane.setPreferredSize(new Dimension(100,20));
          buttonPane.add(new JButton("Button1"));
          buttonPane.add(new JButton("Button2"));
  
          frame.getContentPane().add(controlPane, BorderLayout.NORTH);
          frame.getContentPane().add(buttonPane, BorderLayout.SOUTH);
          frame.setSize(new Dimension(500,500));
          frame.pack();
      }
  }
  </code></pre>
  
  <p>Whatever I do, if I use a grid Layout, it seems to always allocate half of the available space to each control. I have been told that:</p>
  
  <blockquote>
    <p>The height of each row is dependent on the height of each component
    added in each row.</p>
  </blockquote>
  
  <p>The buttonpane's height is 20. It's allocating much more than that to it:</p>
  
  <p><img src="https://i.stack.imgur.com/Vh5eb.png" alt="Wasted space"></p>
  
  <p>What's wrong with this code?<br>
  I would like to leave the two JPanels intact please. It's easy to simply add the textbox and the buttons directly to the frame, but I need to do it with JPanels (because I will be adding borders and other things).</p>