Search code examples
javaswingjscrollpanenull-layout-manager

Adding a JScrollPane removes all the components inside JPanel


When I add a JScrollPane to a JPanel, all the buttons inside the JPanel go away and it becomes empty. I was following this post here as well as other similar ones.

Here is the code without the JScrollPane:

    JPanel panel = new JPanel();
    panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    panel.setBounds(5, 5, 286, 573);
    contentPane.add(panel);
    panel.setLayout(null);

    JPanel panel_1 = new JPanel();
    panel_1.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    panel_1.setBounds(12, 53, 262, 484);
    panel.add(panel_1);
    panel_1.setLayout(null);

    //this panel is the one the scrollPane is being added to and contains the buttons
    JPanel panel_2 = new JPanel();
    panel_2.setBounds(12, 68, 238, 403);
    panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.Y_AXIS));

    for(int i = 0; i < 50; i++) {
        JButton bttn = new JButton("TEST");
        panel_2.add(bttn);
        panel_2.revalidate();

    }
    panel_1.add(panel_2);

Which gives:

And the result is:

And the code with JScrollPane, the last line replaced with this:

JScrollPane scrollPane = new JScrollPane(panel_2);
panel_1.add(scrollPane);
//panel_1.add(panel_2);

However, all the buttons disappear:

All of the buttons disappered

EDIT: Here is the full code:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.SpringLayout;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;


public class Test extends JFrame {


    private JPanel contentPane;

    public static void main(String[] args) {
        Test frame = new Test();
        frame.setVisible(true);
    }


    /**
     * Create the frame.
     */
    public Test() {
        setTitle("Test Program");
        setResizable(false);
        setBackground(Color.WHITE);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 871, 630);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        SpringLayout sl_contentPane = new SpringLayout();
        contentPane.setLayout(sl_contentPane);

        JPanel panel = new JPanel();
        sl_contentPane.putConstraint(SpringLayout.NORTH, panel, 5, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.WEST, panel, 5, SpringLayout.WEST, contentPane);
        sl_contentPane.putConstraint(SpringLayout.SOUTH, panel, 578, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.EAST, panel, 291, SpringLayout.WEST, contentPane);
        panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
        contentPane.add(panel);
        SpringLayout sl_panel = new SpringLayout();
        panel.setLayout(sl_panel);

        JPanel panel_1 = new JPanel();
        sl_panel.putConstraint(SpringLayout.NORTH, panel_1, 53, SpringLayout.NORTH, panel);
        sl_panel.putConstraint(SpringLayout.WEST, panel_1, 12, SpringLayout.WEST, panel);
        sl_panel.putConstraint(SpringLayout.SOUTH, panel_1, 537, SpringLayout.NORTH, panel);
        sl_panel.putConstraint(SpringLayout.EAST, panel_1, 274, SpringLayout.WEST, panel);
        panel_1.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
        panel.add(panel_1);
        SpringLayout sl_panel_1 = new SpringLayout();
        panel_1.setLayout(sl_panel_1);

        JSeparator separator = new JSeparator();
        sl_panel_1.putConstraint(SpringLayout.NORTH, separator, 54, SpringLayout.NORTH, panel_1);
        sl_panel_1.putConstraint(SpringLayout.WEST, separator, 12, SpringLayout.WEST, panel_1);
        sl_panel_1.putConstraint(SpringLayout.SOUTH, separator, 69, SpringLayout.NORTH, panel_1);
        sl_panel_1.putConstraint(SpringLayout.EAST, separator, 250, SpringLayout.WEST, panel_1);
        panel_1.add(separator);

        JLabel lblNewLabel = new JLabel("Results:");
        sl_panel_1.putConstraint(SpringLayout.NORTH, lblNewLabel, 13, SpringLayout.NORTH, panel_1);
        sl_panel_1.putConstraint(SpringLayout.WEST, lblNewLabel, 12, SpringLayout.WEST, panel_1);
        sl_panel_1.putConstraint(SpringLayout.SOUTH, lblNewLabel, 55, SpringLayout.NORTH, panel_1);
        sl_panel_1.putConstraint(SpringLayout.EAST, lblNewLabel, 115, SpringLayout.WEST, panel_1);
        lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 21));
        panel_1.add(lblNewLabel);

        JPanel panel_2 = new JPanel();
        panel_2.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
        sl_panel_1.putConstraint(SpringLayout.NORTH, panel_2, -2, SpringLayout.SOUTH, separator);
        sl_panel_1.putConstraint(SpringLayout.WEST, panel_2, 2, SpringLayout.WEST, lblNewLabel);
        sl_panel_1.putConstraint(SpringLayout.SOUTH, panel_2, 398, SpringLayout.SOUTH, separator);
        sl_panel_1.putConstraint(SpringLayout.EAST, panel_2, 0, SpringLayout.EAST, separator);
        panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.Y_AXIS));
        for(int i = 0; i < 50; i++) {
            JButton bttn = new JButton("TEST");
            panel_2.add(bttn);
        }   

        JScrollPane scrollPane = new JScrollPane(panel_2);
        sl_panel_1.putConstraint(SpringLayout.NORTH, scrollPane, 0, SpringLayout.NORTH, panel_1);
        sl_panel_1.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, panel_1);
        sl_panel_1.putConstraint(SpringLayout.SOUTH, scrollPane, 0, SpringLayout.NORTH, panel_1);
        sl_panel_1.putConstraint(SpringLayout.EAST, scrollPane, 0, SpringLayout.WEST, panel_1);
        panel_1.add(scrollPane);

        //panel_1.add(panel_2); if this is used instead of the above 6 lines of scrollPane, then the buttons appear

    }
}

Here is the image of the overall application; the panel underneath the Results tab with the "TEST" buttons is where the scroll pane should be:

enter image description here

Any help will be much appreciated!


Solution

  • your constraints are a bit strange:

        sl_panel_1.putConstraint(SpringLayout.NORTH, scrollPane, 0, SpringLayout.NORTH, panel_1);
        sl_panel_1.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, panel_1);
        sl_panel_1.putConstraint(SpringLayout.SOUTH, scrollPane, 0, SpringLayout.NORTH, panel_1);
        sl_panel_1.putConstraint(SpringLayout.EAST, scrollPane, 0, SpringLayout.WEST, panel_1);
    

    change to, e.g.:

        sl_panel_1.putConstraint(SpringLayout.NORTH, scrollPane, 0, SpringLayout.SOUTH, separator);
        sl_panel_1.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, panel_1);
        sl_panel_1.putConstraint(SpringLayout.SOUTH, scrollPane, 0, SpringLayout.SOUTH, panel_1);
        sl_panel_1.putConstraint(SpringLayout.EAST, scrollPane, 0, SpringLayout.EAST, panel_1);
    

    and do not add constraints for panel_2 to sl_panel_1. enter image description here

    if you had added the panel_2 with these constraints and no scroll pane, it would not have been displayed too