• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 723
  • Last Modified:

ScrollPane Hides JPanel

The attached code adds a JPanel to a Container.  The JPanel has a lineborder, and can be seen when I invoke the class.  However, when I add a ScrollPane, the JPanel disappears, and I'm shown a blank GUI screen.

Please note the lines marked #1, #2 & #3 in the attached code.  When I run the code with the ArithmeticLayoutManager layout manager used, #1, the problem occurs.  When I run the code with the GridLayout layout manager used, #2, the same problem occurs.  

And, when I comment out line #3, so the ScrollPane is not created, the JPanel shows up on the GUI screen fine.

Why is the ScrollPane hiding my JPanel?
package mugs;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import nl.marcoslot.lib.gui.layout.alm.ArithmeticLayoutManager;
public class Practice extends JFrame {
    Container panel = null;
    JPanel pnlMugs = null;
    public Practice() {
    public void init() {
        panel = getContentPane();
        panel.setLayout(new ArithmeticLayoutManager()); // #1: Layout #1
        //panel.setLayout(new GridLayout());            // #2: Layout #2
        JScrollPane sp = new JScrollPane(panel);        // #3: Adding scrollpane
          pnlMugs = new JPanel();
        pnlMugs.setPreferredSize(new Dimension(570,750));
        LineBorder line = new LineBorder(Color.black, 1, true);
        setSize(700, 850);
    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Practice().setVisible(true);

Open in new window

3 Solutions
I would expect you to add a JScrollPane to the content pane:

JPanel p = ...................
JScrollPane s = new JScrollPane(p);


And then .... which layout for the content pane?

Of course the layoutmanager for the content pane is set before adding components to it.

your panel disappears because you add it to your scroll pane (which would remove it from your gui (a component can only have one parent).
you then don't add your scroll to your gui.

try what sciuriware has suggested above
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

LarryAndroAuthor Commented:
The scuriware approach worked.  Thanks, both of you!

(I submitted points on this question several hours ago.  I don't know why my submission didn't "take."  :(    )
LarryAndroAuthor Commented:
Something is wrong with the Experts-Exchange website, or I don't understand their interface.  Yesterday, I closed call with a comment, and it didn't happen and the comment wasn't recorded.  Later, I closed call again with comment.  That time, the call was closed.  But, I don't see my comment.  Hmmm...
Please post your original comment again.
May be you hit the wrong button,
LarryAndroAuthor Commented:
The comment didn't add anything technically.  Both times I closed the call, adding a comment during the process saying basically that "it worked."  
LarryAndroAuthor Commented:
Every comment I add, when I add only comments shows up for review.  The comments added when I close the question don't show up.  Maybe this is a feature and not a bug.  In the future, I will add a comment, and as a second action close the question; two separate actions.
I'm afraid that closing and adding comments can't be done at the same time.
Don't worry.


Experts Exchange has added a feature to the closing process that gives the Asker an opportunity to fill out a survey on the question and leave a comment; however, that comment appears only to the people receiving points and the Asker (I don't even see them at this point).

Site Admin
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now