title: Stop expansion of JScrollPane within a BoxLayout
body: <p>The <code>JScrollPane</code> in the <code>JPanel</code> below expands to fill the Frame. How can I stop this expansion so that the size of the <code>JScrollPane</code> is the size of the <code>JButton</code> within it?</p>
  
  <pre><code>  public class GUITest extends JFrame {
  
      public GUITest() {
          setPreferredSize(new Dimension(700, 500));
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          JPanel mainPanel = new JPanel();
          mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
          add(mainPanel);
  
          JButton button1 = new JButton();
          JButton button2 = new JButton();
          JButton button3 = new JButton();
  
          JScrollPane pane1 = new JScrollPane();
          mainPanel.add(button1);
          mainPanel.add(pane1);
          pane1.setViewportView(button2);
          mainPanel.add(button3);
  
          //Display the window.
          super.pack();
          super.setLocationRelativeTo(null);
          super.setVisible(true);
      }
  }
  </code></pre>
  
  <p>I tried  </p>
  
  <pre><code>pane1.setMaximumSize(button2.getPreferredSize());
  </code></pre>
  
  <p>but the scrollbars appear and I can't see the button. Also, in my real program, the components within the <code>JScrollPane</code> will be dynamically added during runtime so I will need the <code>JScrollPane</code> to expand for this.</p>