-1

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!

Tejas Sharma
  • 89
  • 12
  • 1
    Please edit your question to include a [mcve] that illustrates the problem you want to solve _without_ a `null` layout. – trashgod Jun 25 '17 at 07:45
  • 2
    `panel.setLayout(null);` Null layouts and scroll panes do not work together. More generally, Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). - Post the MCVE as advised by @trashgod for further advice. – Andrew Thompson Jun 25 '17 at 08:16
  • So, if panel2 has a layout manager, but if it is in panel1 that does not have a layout manager then a scroll pane won't be able to work with panel2? If that is the case, I guess that is the answer to my question. – Tejas Sharma Jun 25 '17 at 08:51
  • Updated the question so SpringLayout is being used for contentPane and all the panels. The problem still persists. Removing the scrollPane code and replacing it with panel_1.add(panel_2) still gives me buttons. – Tejas Sharma Jun 25 '17 at 09:24
  • *"Updated the question.."* Tip: Add @trashgod (or whoever, the `@` is important) to *notify* the person of a new comment. *"The problem still persists."* It will likely stay that way until you post code we can work with. Where is the MCVE? – Andrew Thompson Jun 25 '17 at 10:52
  • @AndrewThompson The code after the "EDIT" at the bottom (it looks big because of the SpringLayout stuff). – Tejas Sharma Jun 25 '17 at 12:21
  • 1
    An [mcve], for [example](https://stackoverflow.com/a/15078211/230513), [example](https://stackoverflow.com/a/3175280/230513), [example](https://stackoverflow.com/a/7229662/230513). – trashgod Jun 25 '17 at 22:06
  • *"The code after the "EDIT" at the bottom"* I saw it. That code has no imports, no `main` method, no class definition.. As such it is an uncompilable code snippet that does not honor the C, V or E of MCVE. Have you even read the link offered? – Andrew Thompson Jun 26 '17 at 00:34
  • @AndrewThompson Added the code with all the relevant bits (including the main and the class). Sorry for that, I thought that much code was all that was required in order to solve the problem as in here [link](https://stackoverflow.com/questions/29873318/glew-does-not-init); my misunderstanding. Thanks for sticking with me! – Tejas Sharma Jun 26 '17 at 05:20
  • I detest spring layout! Would a variant using other layouts work for you? Provide ASCII art or a simple drawing of the *intended* layout of the GUI at preferred size. BTW - What do all the buttons do, would that part of the GUI be workable as a list (`JList`)? The `JList` component provides many advantages over using a collection of buttons, including that we can set a number of visible rows for it as an indication of the best height to report to scroll panes and layout managers. – Andrew Thompson Jun 27 '17 at 20:56
  • @AndrewThompson Edited the question with the image of the overall application, and the panel underneath the Results tab with the "TEST" buttons is where the scroll pane should be. The buttons are supposed to be clicked on to do an action. I just need to know why the scroll pane Is not working and I will be able to implement it. – Tejas Sharma Jun 28 '17 at 11:31

1 Answers1

2

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

user85421
  • 28,957
  • 10
  • 64
  • 87