title: JScrollPane resize containing JPanel when scrollbars appear
body: <p>I have a small problem when using <code>JScrollPane</code> in my Java application.</p>
  
  <p>I have a <code>JScrollPane</code> containing a <code>JPanel</code>.
  This <code>JPanel</code> is dynamically updated with buttons (vertically ordered) that can be of any width.
  The <code>JPanel</code> automatically adjusts its width to the largest <code>JButton</code> component inside.</p>
  
  <p>Now when the vertical scrollbar appears, it takes away some space on the right side of my <code>JPanel</code>, which causes the largest buttons not to appear completely. I don't want to use a horizontal scrollbar in addition to display the whole button.</p>
  
  <p>Is there a way to resize my <code>JPanel</code> when a scrollbar appears, so it appears nicely next to my buttons? Or is there any other way to have the scrollbar appear next to my <code>JPanel</code>?</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>EDIT</strong>: Here is a demo of my problem. When you resize the window to a smaller height, a little part of the buttons on the right side gets covered.</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.EventQueue;
  
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import java.awt.GridLayout;
  
  /**
   * @author Dylan Kiss
   */
  
  public class Demo {
      public static void main(String[] args) {
          EventQueue.invokeLater(new Runnable() {
              @Override
              public void run() {
                  try {
                      JFrame myFrame = new JFrame("Demo");
                      JPanel sideBar = new JPanel();
                      JPanel centerPanel = new JPanel();
                      centerPanel.add(new JLabel("This is the center panel."));
  
                      JPanel buttonContainer = new JPanel();
                      JButton myButton = null;
  
                      for (int i = 0; i &lt; 20; i++) {
                          buttonContainer.setLayout(new GridLayout(20, 1, 0, 0));
                          myButton = new JButton("This is my button nr. " + i);
                          buttonContainer.add(myButton);
                      }
  
                      sideBar.setLayout(new BorderLayout(0, 0));
  
                      JScrollPane scrollPane = new JScrollPane(buttonContainer);
  
                      sideBar.add(scrollPane);
  
                      myFrame.getContentPane().add(sideBar, BorderLayout.WEST);
                      myFrame.getContentPane().add(centerPanel, BorderLayout.CENTER);
  
                      myFrame.setLocationByPlatform(true);
                      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      myFrame.pack();
                      myFrame.setVisible(true);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          });
      }
  }
  </code></pre>