Solved

ScrollPane Hides JPanel

Posted on 2007-11-26
11
710 Views
Last Modified: 2013-11-23
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() {
        init();
    }
    
    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);
        pnlMugs.setBorder(line);
        pnlMugs.setOpaque(false);
        
        panel.add(pnlMugs,"x=10;y=10");
        
        setSize(700, 850);
        setTitle("Practice");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
 
    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Practice().setVisible(true);
            }
        });
    }
    
}

Open in new window

0
Comment
Question by:LarryAndro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 24

Accepted Solution

by:
sciuriware earned 450 total points
ID: 20350901
I would expect you to add a JScrollPane to the content pane:

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

getContentPane().add(s);

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

;JOOP!
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 450 total points
ID: 20350907
Of course the layoutmanager for the content pane is set before adding components to it.

;JOOP!
0
 
LVL 92

Assisted Solution

by:objects
objects earned 50 total points
ID: 20353558
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Closing Comment

by:LarryAndro
ID: 31410989
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."  :(    )
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20356089
:))
0
 
LVL 1

Author Comment

by:LarryAndro
ID: 20357574
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...
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20357934
Please post your original comment again.
May be you hit the wrong button,
;JOOP!
0
 
LVL 1

Author Comment

by:LarryAndro
ID: 20357992
The comment didn't add anything technically.  Both times I closed the call, adding a comment during the process saying basically that "it worked."  
0
 
LVL 1

Author Comment

by:LarryAndro
ID: 20358006
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.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20358217
I'm afraid that closing and adding comments can't be done at the same time.
Don't worry.

;JOOP!
0
 
LVL 5

Expert Comment

by:Netminder
ID: 20390433
Folks,

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

Netminder
Site Admin
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question