1

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:

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 < 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);

    }

}

enter image description here

Update using JList

It didn't work!

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 < 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);

    }

}

Problem Solved: Working 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 < 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);
    }

}
Saad A
  • 1,135
  • 2
  • 21
  • 46
  • 1
    What will ultimately appear in the '20 panels'? The 'Library items' suggests either a `JList` with appropriate renderer or a `JTable` would be better for this purpose. – Andrew Thompson Feb 27 '17 at 13:30
  • each of 20 panel would contain a form elements to display day and few buttons that would provide addition functionality. – Saad A Feb 27 '17 at 13:34
  • 1
    That could be achieved with a single selection `JList` with each list item showing the date, with the buttons outside (e.g. below) the list (outside the scroll pane). – Andrew Thompson Feb 27 '17 at 13:43
  • I am a little confused can you provide a very simple example – Saad A Feb 27 '17 at 13:54
  • *"I am a little confused .."* Which part are you having trouble understanding? *"can you provide a very simple example"* I could provide an example, though whether it is 'simple' is up to the observer to judge (what I think is simple might not be what you think is simple). But I won't provide a code sample because SO is not a code generation machine. – Andrew Thompson Feb 27 '17 at 14:15
  • fair enough, but instead of me finding a who new solution to the problem, could you perhaps point out why my current code is not working? – Saad A Feb 27 '17 at 14:18
  • 1
    *"could you perhaps point out why my current code is not working?"* `contentPane = new JPanel(null);` and `contentPane.setPreferredSize(...);` see [this question](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi), the answers in [this question](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) and [null layout is evil](http://www.leepoint.net/GUI/layouts/nulllayout.html). At first view that's what I think is causing problems – Frakcool Feb 27 '17 at 14:22
  • Ultimately I want each one of the white boxes inside the scrollable panel to be JPanel, so I should create a JList of JPanels and add each sp1 component to it? – Saad A Feb 27 '17 at 14:29
  • Do you still need help? You edited your question with a *"Working code"* text, but not sure if it was the "solved" code or the "I still need help with this code"... – Frakcool Feb 27 '17 at 17:25

1 Answers1

1

Add p1 into contentPane, not into frame The example above is good. Have a nice day.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;

public class DisplayItems3 {

    public static void main(final 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 GridLayout(-1, 1));
        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(new BorderLayout());
        contentPane.setPreferredSize(new Dimension(950, 800));
        contentPane.add(scrollPane, BorderLayout.CENTER);

        for (int i = 0; i < 20; i++) {

            JPanel sp1 = new JPanel();
            sp1.setLayout(new FlowLayout());
            sp1.setBackground(Color.WHITE);
            sp1.setPreferredSize(new Dimension(900, 180));
            sp1.setBorder(new LineBorder(Color.RED));

            p2.add(sp1);

        }

        // contentPane.add(p2);
        contentPane.add(p1, BorderLayout.NORTH);
        // frame.add(p2);
        // frame.setContentPane(contentPane);
        frame.add(contentPane);

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);

    }

}