title: Java Swing Scroll-able JPanel with Multiple Panels on Frame
body: <p>I have a JFrame and it has two JPanels. First is the header and then below it is the main content panel. I want to make my second content panel to be scroll-able but even though the scrollbar appears on the panel it doesn't seem to expand to its true height. In my second JPanel called p2, I am adding 20 small JPanels which should cause it to expand in height but it doesn't do that.  Below is my Code:</p>
  
  <pre><code>public class DisplayItems3{
  
      public static void main(String[] args) {
  
          JFrame frame = new JFrame();
  
          final int FRAME_WIDTH  = 1000;
          final int FRAME_HEIGHT = 1000;
  
          frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
          frame.setTitle("Home Library");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLayout(new FlowLayout());
  
          //Top Panel
          JPanel p1 = new JPanel();
          p1.setLayout(new FlowLayout());
          p1.setBackground(Color.LIGHT_GRAY);
          p1.setPreferredSize(new Dimension(950, 100));
  
          JLabel l1 = new JLabel("All Library Items");
          l1.setForeground(Color.BLACK);
          l1.setPreferredSize(new Dimension(900, 50));
          l1.setFont(l1.getFont().deriveFont(30.0f));
  
          p1.add(l1);
  
          //Content Panel
          JPanel p2 = new JPanel();
          p2.setLayout(new FlowLayout());
          p2.setBackground(Color.LIGHT_GRAY);
          p2.setPreferredSize(new Dimension(950, 800));
          p2.setAutoscrolls(true);
  
          JScrollPane scrollPane = new JScrollPane(p2);
          scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
          scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
          scrollPane.setBounds(0, 0, 950, 800);
  
          JPanel contentPane = new JPanel(null);
          contentPane.setPreferredSize(new Dimension(950, 800));
          contentPane.add(scrollPane);
  
          for(int i = 0; i &lt; 20; i++) {
  
              JPanel sp1 = new JPanel();
              sp1.setLayout(new FlowLayout());
              sp1.setBackground(Color.WHITE);
              sp1.setPreferredSize(new Dimension(900, 180));
  
              p2.add(sp1);    
  
          }
  
  
          //contentPane.add(p2);
          frame.add(p1);
          //frame.add(p2);
          //frame.setContentPane(contentPane);
          frame.add(contentPane);
  
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          frame.setVisible(true);
  
      }
  
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/BaJ1d.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BaJ1d.png" alt="enter image description here"></a></p>
  
  <p><strong>Update using JList</strong></p>
  
  <p>It didn't work!</p>
  
  <pre><code>public class JListTest {
  
      public static void main(String[] args) {
  
          JFrame frame = new JFrame();
  
          final int FRAME_WIDTH  = 1000;
          final int FRAME_HEIGHT = 1000;
  
          frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
          frame.setTitle("Home Library");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLayout(new FlowLayout());
  
          //Top Panel
          JPanel p1 = new JPanel();
          p1.setLayout(new FlowLayout());
          p1.setBackground(Color.LIGHT_GRAY);
          p1.setPreferredSize(new Dimension(950, 100));
  
          JLabel l1 = new JLabel("All Library Items");
          l1.setForeground(Color.BLACK);
          l1.setPreferredSize(new Dimension(900, 50));
          l1.setFont(l1.getFont().deriveFont(30.0f));
  
          p1.add(l1);
  
          JList ll1 = new JList();
          ll1.setLayout(new FlowLayout());
          ll1.setBackground(Color.LIGHT_GRAY);
          ll1.setPreferredSize(new Dimension(950, 800));
          ll1.setAutoscrolls(true);
  
          JScrollPane scrollPane = new JScrollPane(ll1);
          scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
          scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
          scrollPane.setBounds(0, 0, 950, 800);
  
          for(int i = 0; i &lt; 20; i++) {
  
              JPanel sp1 = new JPanel();
              sp1.setLayout(new FlowLayout());
              sp1.setBackground(Color.WHITE);
              sp1.setPreferredSize(new Dimension(900, 180));
  
              ll1.add(sp1);    
  
          }
  
  
          //contentPane.add(p2);
          frame.add(p1);
          frame.add(scrollPane);
          //frame.setContentPane(contentPane);
          //frame.add(contentPane);
  
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          frame.setVisible(true);
  
      }
  
  }
  </code></pre>
  
  <p><strong>Problem Solved: Working Code</strong></p>
  
  <pre><code>public class scrollTest2 {
  
      public static void main(String[] args)
      {
          JFrame frame = new JFrame();
  
          final int FRAME_WIDTH  = 1000;
          final int FRAME_HEIGHT = 1000;
  
          frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
          frame.setTitle("Home Library");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLayout(new BorderLayout());
  
          JLabel m1 = new JLabel("safsd");
  
          JPanel panel = new JPanel();
          panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
          panel.setAutoscrolls(true);
          frame.add(panel,BorderLayout.NORTH);
  
          JScrollPane scrollPane = new JScrollPane(panel);
          scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
          scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
          scrollPane.setBounds(50, 30, 800, 800);
  
          JPanel contentPane = new JPanel(null);
          contentPane.setPreferredSize(new Dimension(900, 900));
          contentPane.add(scrollPane);
  
          for(int i = 0; i &lt; 30; i++) {
  
              JPanel sp1 = new JPanel();
              sp1.setLayout(new FlowLayout());
              sp1.setBackground(Color.WHITE);
              sp1.setPreferredSize(new Dimension(900, 180));
  
              JPanel ssp1 = new JPanel();
              ssp1.setLayout(new FlowLayout());
              ssp1.setBackground(Color.WHITE);
              ssp1.setPreferredSize(new Dimension(500, 170));
  
              JPanel ssp2 = new JPanel();
              ssp2.setLayout(new FlowLayout());
              ssp2.setBackground(Color.WHITE);
              ssp2.setPreferredSize(new Dimension(350, 170));
  
              JLabel l3 = new JLabel("Title: ");
              l3.setForeground(Color.BLACK);
              l3.setPreferredSize(new Dimension(100, 20));
              JTextField t1 = new JTextField("Electronic Basics");
              t1.setPreferredSize(new Dimension(320, 20));
  
              JLabel l4 = new JLabel("Type: ");
              l4.setForeground(Color.BLACK);
              l4.setPreferredSize(new Dimension(100, 20));
              JTextField t2 = new JTextField("Book");
              t2.setPreferredSize(new Dimension(320, 20));
  
              JLabel l5 = new JLabel("Authors: ");
              l5.setForeground(Color.BLACK);
              l5.setPreferredSize(new Dimension(100, 20));
              JTextField t3 = new JTextField("Bob the Builder");
              t3.setPreferredSize(new Dimension(320, 20));
  
              JLabel l6 = new JLabel("Publisher: ");
              l6.setForeground(Color.BLACK);
              l6.setPreferredSize(new Dimension(100, 20));
              JTextField t4 = new JTextField("ABC Company");
              t4.setPreferredSize(new Dimension(320, 20));
  
              JLabel l7 = new JLabel("Location: ");
              l7.setForeground(Color.BLACK);
              l7.setPreferredSize(new Dimension(100, 20));
              JTextField t5 = new JTextField("Shelf 1 Row 3");
              t5.setPreferredSize(new Dimension(320, 20));
  
              JLabel l8 = new JLabel("Status: ");
              l8.setForeground(Color.BLACK);
              l8.setPreferredSize(new Dimension(100, 20));
              JTextField t6 = new JTextField("Available");
              t6.setPreferredSize(new Dimension(320, 20));
  
              JButton btnLoanHistory = new JButton("Loan History");
              btnLoanHistory.setPreferredSize(new Dimension(300, 20));
              JButton btnLoanItem = new JButton("Loan Item");
              btnLoanItem.setPreferredSize(new Dimension(300, 20));
              JButton btnProcessReturn = new JButton("Process Return");
              btnProcessReturn.setPreferredSize(new Dimension(300, 20));
  
              ssp1.add(l3);
              ssp1.add(t1);
              ssp1.add(l4);
              ssp1.add(t2);
              ssp1.add(l5);
              ssp1.add(t3);
              ssp1.add(l6);
              ssp1.add(t4);
              ssp1.add(l7);
              ssp1.add(t5);
              ssp1.add(l8);
              ssp1.add(t6);
  
              ssp2.add(btnLoanHistory);
              ssp2.add(btnLoanItem);
              ssp2.add(btnProcessReturn);
  
              sp1.add(ssp1);
              sp1.add(ssp2);
              panel.add(sp1); 
  
          }
  
          frame.add(m1, BorderLayout.NORTH);
          frame.add(contentPane, BorderLayout.CENTER);
          frame.pack();
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          frame.setVisible(true);
      }
  
  }
  </code></pre>