title: Best practice to resize JScrollPane
body: <p>I read some answered questions in this forum (<a href="https://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi">this one</a> for example) where it is strictly recommended to avoid the use of setXXXSize() methods to resize components in swing applications.</p>
  
  <p>So, coming to my problem, i would like to know how to best resize a JScrollPane in order to avoid its parent panel to increase its size without any control.</p>
  
  <p>Before writing some code, i want to describe the real situation, since i will post a "toy example".</p>
  
  <p>In my JFrame i'm currently using a border layout for my content pane. At BorderLayout.CENTER there is a JPanel where i do some custom painting.
  At BorderLayout.EAST there is a JPanel (say eastPanel) containing some components inside another panel (this panel will be added to eastPanel at BorderLayout.NORTH), and a JScrollPane which contains a JTable (added to eastPanel at BorderLayout.CENTER). This table will have a lot of rows.
  Since i want eastPanel's height to be the same as centerPanel's height, i need some way to avoid the JScrollPane to increase its size in order to try to display as much rows as possible.</p>
  
  <p>For now i wasn't be able to find another solution apart from calling setPreferredSize on the eastPanel containing the scrollpane, but i have to admit that i hate this kind of solution.</p>
  
  <p><strong>Sample Code</strong></p>
  
  <p>In this code sample i added some random labels at the north of eastPanel and inside the JScrollPane, since my purpose was to post a short sample of code.
  However, the situation is very similar to the one i have described above.
  I wasn't be able to solve my problem without using this "terrible" line of code :</p>
  
  <pre><code>eastPanel.setPreferredSize(new Dimension(eastPanel.getPreferredSize().width, centerPanel.getPreferredSize().height));
  </code></pre>
  
  <p>I would like to avoid a more complex layout for a simple situation like this. Am i missing something ? Also, is setting that empty border an acceptable way to set the size of the panel where i will do some custom painting?</p>
  
  <p>Code :</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  import javax.swing.border.EmptyBorder;
  public class Test
  {
      public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  try {                       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                      new TestFrame().setVisible(true);
                  }
                  catch(Exception exception) {
                      JOptionPane.showMessageDialog(null, "Fatal error while initialiing application", "Error", JOptionPane.ERROR_MESSAGE);
                  }
              }
          });
      }
  }
  class TestFrame extends JFrame 
  {
      public TestFrame() {
          super("Test");
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          JPanel pane = new JPanel(new BorderLayout(20, 0));
          pane.setBorder(new EmptyBorder(20, 20, 20, 20));
          JPanel centerPanel = new JPanel();
          centerPanel.setBackground(Color.WHITE);
          centerPanel.setBorder(new EmptyBorder(400, 400, 0, 0));
          // centerPanel.setPreferredSize(new Dimension(400, 400));
          JPanel eastPanel = new JPanel(new BorderLayout(0, 20));
          JPanel labelsContainer = new JPanel(new GridLayout(0, 1));
          for(int i=0;i&lt;7;i++) labelsContainer.add(new JLabel(String.valueOf(i)));
          eastPanel.add(labelsContainer, BorderLayout.NORTH);
          JPanel moreLabelsContainer = new JPanel(new GridLayout(0, 1));
          for(int i=7;i&lt;70;i++) moreLabelsContainer.add(new JLabel(String.valueOf(i)));
          JScrollPane scroll = new JScrollPane(moreLabelsContainer, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
          eastPanel.add(scroll, BorderLayout.CENTER);
          eastPanel.setPreferredSize(new Dimension(eastPanel.getPreferredSize().width, centerPanel.getPreferredSize().height));
          pane.add(centerPanel, BorderLayout.CENTER);
          pane.add(eastPanel, BorderLayout.EAST);
          setContentPane(pane);
          pack();
          setLocationRelativeTo(null);
      }
  }
  </code></pre>
  
  <p>Thanks for your help !</p>