Link to home
Start Free TrialLog in
Avatar of xenia27
xenia27Flag for Taiwan, Province of China

asked on

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
SOLUTION
Avatar of expertmb
expertmb

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mick Barry
why aren't you using a layout manager?
Make sure you are adding scPanel to your component hierarchy, and not basicPanel.
Avatar of xenia27

ASKER

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...
Avatar of expertmb
expertmb

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);
            }
      }
}
> 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.
Avatar of xenia27

ASKER

so should I set viewport???
personally I'd use a layout manager
Avatar of xenia27

ASKER

so which Layout manager is preferred???
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
a BoxLayout would seem suitable for you.
Did setting the preferred size help?
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.
Avatar of xenia27

ASKER

OK...it works...I  mean setPreferredSize...^^...Thanks~