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

JScrollPane and JPanel

Hi,

I got this problem...I try to put some small JPanel into a bigger JPanel, and put the bigger one into a JScrollPane in case if the bigger one cannot show all components inside...but now I cannot see all the components which are inside of the bigger JPanel...and no scroll bar displayed...Please help me to exam my codes...


JPanel basicPanel = new JPanel();
JPanel comPanel[] = new JPanel[6];
JScrollPane scPanel = new JScrollPane(basicPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

basicPanel.setLayout(null);
basicPanel.setSize(850, 400);
basicPanel.setBackground(Color.black);

for (int i = 0; i < 6; i++)
{
 JLabel tmpLabel = new JLabel("Panel " + String.valueOf(i));
 comPanel[i] = new JPanel();
 comPanel[i].add(tmpLabel);
 basicPanel.add(comPanel[i]);
 comPanel[i].setBounds(0, i*100, 300, 100);
}



so what should I do???



Xenia
0
xenia27
Asked:
xenia27
  • 6
  • 4
  • 3
2 Solutions
 
expertmbCommented:
it will display scrollbar always
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
0
 
objectsCommented:
why aren't you using a layout manager?
0
 
objectsCommented:
Make sure you are adding scPanel to your component hierarchy, and not basicPanel.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
xenia27Author Commented:
OK...I try JScrollPane.VERTICAL_SCROLLBAR_ALWAYS and JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
I do saw the scroll panel but I cannot scroll down as it should be...Any idea?

Just need to align all the smaller JPanel by myself...
0
 
expertmbCommented:
import javax.swing.*;
import java.awt.*;
class scrlpaneTest extends JFrame{

      public static void main(String[] s){
            scrlpaneTest t = new scrlpaneTest();
            t.setSize(400,400);
            t.setVisible(true);
      }

      scrlpaneTest(){
            JPanel basicPanel = new JPanel();
            JPanel comPanel[] = new JPanel[6];
            JScrollPane scPanel = new JScrollPane(basicPanel,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

            basicPanel.setLayout(null);
            basicPanel.setSize(850, 400);
            basicPanel.setBackground(Color.black);

            getContentPane().add(scPanel, BorderLayout.CENTER);
            for (int i = 0; i < 6; i++)
            {
             JLabel tmpLabel = new JLabel("Panel " + String.valueOf(i));
             comPanel[i] = new JPanel();
             comPanel[i].add(tmpLabel);
             basicPanel.add(comPanel[i]);
             comPanel[i].setBounds(0, i*100, 300, 100);
            }
      }
}
0
 
objectsCommented:
> I do saw the scroll panel but I cannot scroll down as it should be...Any idea?

that is because your scroll pane thinks the panel is smaller than the viewport.

A layout manager make all this a lot easier, as it handles all the required sizing for you.
0
 
xenia27Author Commented:
so should I set viewport???
0
 
objectsCommented:
personally I'd use a layout manager
0
 
xenia27Author Commented:
so which Layout manager is preferred???
0
 
objectsCommented:
if not using a layout manager try setting the preferred size of your panel:

basicPanel.setPreferredSize(new Dimension(850, 400));
0
 
objectsCommented:
a BoxLayout would seem suitable for you.
Did setting the preferred size help?
0
 
expertmbCommented:
problem with the basicPanel setSize or preffered size
try to give different value for X you will see the scroll bar effect
basicPanel.setPreferredSize(new Dimension(100, X));

moreover its null layout which is making the panels to appear till the size of the panel height and width,
after that panels will not be visible.
0
 
xenia27Author Commented:
OK...it works...I  mean setPreferredSize...^^...Thanks~
0
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

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now