Scrolling a JPanel
I have a JPanel which contains smaller JPanels, I want this panel to be scrollable, I tried adding it to a JScrollPane but it didn't work.
here is the code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.border.LineBorder;
publicclass TestPanels{
publicstaticvoid main(String[]args)
{
JPanel container =new JPanel();
container.setAutoscrolls(true);
container.setLayout(null);
/*JLabel textLabel_1 = new JLabel("TEST SCROLLING");
textLabel_1.setBounds(10,10,90,15);
JLabel textLabel_2 = new JLabel("TEST SCROLLING");
textLabel_2.setBounds(10,10,90,15);*/
JPanel[] containers =new JPanel[10];
JLabel[] labels =new JLabel[10];
for(int i=0;i<10;i++)
{
containers[i] =new JPanel();
containers[i].setBounds(10,10+(50*i),300, 50);
containers[i].setBorder(new LineBorder(Color.BLUE));
labels[i] =new JLabel("TEST SCROLLING");
labels[i].setBounds(0,0,90,15);
containers[i].add(labels[i]);
container.add(containers[i]);
}
/*JPanel panel_1 = new JPanel();
panel_1.add(textLabel_1);
panel_1.setBorder(new LineBorder(Color.BLUE));
container.add(panel_1);
JPanel panel_2 = new JPanel();
panel_2.add(textLabel_2);
container.add(panel_2);*/
JScrollPane pane =new JScrollPane(container);
pane.setAutoscrolls(true);
pane.setSize(100,50);
/*JViewport view = new JViewport();
view.setView(container);
view.setViewSize(new Dimension(50,50));
pane.setViewportView(view);
pane.setBounds(20,20,200,200);*/
JPanel temp =new JPanel();
temp.setLayout(new BorderLayout());
temp.add(pane,BorderLayout.CENTER);
temp.setSize(50,20);
JFrame test =new JFrame("Test");
test.setBounds(10,10,400,500);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.add(temp);
test.setVisible(true);
}
}
I want to scroll this "container" panel ...

