title: Java how to disable JPanel components automatic vertical resizing
body: <p>I've ran into a pretty annoying problem. The JPanel is adding a vertical gap between components, and I need to get rid of that. 
  I'm trying to get this (Blue lines are the space I want to get rid of):</p>
  
  <p><a href="https://i.stack.imgur.com/pEO4t.png" rel="nofollow"><img src="https://i.stack.imgur.com/pEO4t.png" alt="enter image description here"></a></p>
  
  <p>To look like this:</p>
  
  <p><a href="https://i.stack.imgur.com/WRuw3.png" rel="nofollow"><img src="https://i.stack.imgur.com/WRuw3.png" alt="enter image description here"></a></p>
  
  <p>Here is my current class:</p>
  
  <pre><code>public class SummaryPanel extends JPanel
  {
      private JLabel bagelLabel;
      private JLabel toppingLabel;
      private JLabel coffeeLabel;
      private JLabel shotsLabel;
  
      private JLabel subtotal;
      private JLabel tax;
      private JLabel total;
  
      private JPanel selectionsPanel;
      private JPanel totalPanel;
  
      public SummaryPanel()
      {
          bagelLabel = new JLabel("No bagel $0.00");
          toppingLabel = new JLabel("No topping $0.00");
          coffeeLabel = new JLabel("No coffee $0.00");
          shotsLabel = new JLabel("(Includes 0 shots) $0.00");
  
          subtotal = new JLabel("");
          tax = new JLabel("");
          total = new JLabel("");
  
          setLayout(new GridLayout(2,1));
  
          selectionsPanel = new JPanel();
  
          selectionsPanel.setLayout(new GridLayout(4,1));
          selectionsPanel.add(bagelLabel);
          selectionsPanel.add(toppingLabel);
          selectionsPanel.add(coffeeLabel );
          selectionsPanel.add(shotsLabel );
  
          totalPanel = new JPanel();
          totalPanel.setLayout(new GridLayout(3,1));
          totalPanel.add(subtotal);
          totalPanel.add(tax);
          totalPanel.add(total);
  
          totalPanel.setVisible(false);
  
          add(selectionsPanel);
          add(totalPanel);
      }
  }
  </code></pre>