title: Java Layout for scrollable panels
body: <p>I am trying to create a <code>JPanel</code> that is resizable &amp; scrollable and contains x smaller inner panels. Each inner panel can be as wide as it wants/needs. BUT the depth should be a preferred size.</p>
  
  <p>Like: </p>
  
  <p><img src="https://i.stack.imgur.com/Gvo2s.jpg" alt="enter image description here"></p>
  
  <p>So far my code is:</p>
  
  <pre><code>public class TestSize {
  
      public static void main(String[] args) {
  
          JFrame F = new JFrame();
          F.setVisible(true);
          JPanel P = new JPanel();
          P.setLayout(new BorderLayout());
          JScrollPane scrollPane = new JScrollPane(P);
          scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
          scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
          scrollPane.setPreferredSize(new Dimension(900,900));//.setBounds(50, 30, 300, 50);
  
          JPanel S = new JPanel();
          S.setBackground(Color.GREEN);
          S.setPreferredSize(new Dimension(900,200));
              JPanel S2 = new JPanel();
          S2.setBackground(Color.GREEN);
          S2.setPreferredSize(new Dimension(900,200));
              P.add(S,BorderLayout.NORTH);
              P.add(S2,BorderLayout.NORTH);
  
  
          F.add(scrollPane);
          F.pack();
          F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      }
  
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/Rh9j3.jpg" alt="enter image description here"></p>
  
  <p>But when I have added a single inner panel it fills all the space vertically - which is not what I want:</p>