xenia27
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_SCROL LBAR_AS_NE EDED,JScro llPane.HOR IZONTAL_SC ROLLBAR_AS _NEEDED);
basicPanel.setLayout(null) ;
basicPanel.setSize(850, 400);
basicPanel.setBackground(C olor.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
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_SCROL
basicPanel.setLayout(null)
basicPanel.setSize(850, 400);
basicPanel.setBackground(C
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
why aren't you using a layout manager?
Make sure you are adding scPanel to your component hierarchy, and not basicPanel.
ASKER
OK...I try JScrollPane.VERTICAL_SCROL LBAR_ALWAY S and JScrollPane.HORIZONTAL_SCR OLLBAR_ALW AYS
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...
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...
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_SCROL LBAR_ALWAY S,JScrollP ane.HORIZO NTAL_SCROL LBAR_ALWAY S);
basicPanel.setLayout(null) ;
basicPanel.setSize(850, 400);
basicPanel.setBackground(C olor.black );
getContentPane().add(scPan el, 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);
}
}
}
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_SCROL
basicPanel.setLayout(null)
basicPanel.setSize(850, 400);
basicPanel.setBackground(C
getContentPane().add(scPan
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.
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.
ASKER
so should I set viewport???
personally I'd use a layout manager
ASKER
so which Layout manager is preferred???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
a BoxLayout would seem suitable for you.
Did setting the preferred size help?
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.setPreferredSiz e(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.
try to give different value for X you will see the scroll bar effect
basicPanel.setPreferredSiz
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.
ASKER
OK...it works...I mean setPreferredSize...^^...Th anks~