Advertisement

11.26.2007 at 07:28AM PST, ID: 22982543
[x]
Attachment Details

ScrollPane Hides JPanel

Asked by LarryAndro in Java AWT & Swing, Java Programming Language

Tags: jpanel, scrollpane

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?Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
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);
            }
        });
    }
    
}
[+][-]11.26.2007 at 08:22AM PST, ID: 20350901

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Java AWT & Swing, Java Programming Language
Tags: jpanel, scrollpane
Sign Up Now!
Solution Provided By: sciuriware
Participating Experts: 3
Solution Grade: A
 
 
[+][-]11.26.2007 at 08:23AM PST, ID: 20350907

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]11.26.2007 at 01:58PM PST, ID: 20353558

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628