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 ...

[3776 byte] By [Rami_Magdia] at [2007-11-27 5:59:50]
# 1

The key to the problem is the part that you have commented out:

/*JViewport view = new JViewport();

view.setView(container);

view.setViewSize(new Dimension(50,50));

pane.setViewportView(view);

pane.setBounds(20,20,200,200);*/

Don't create the JViewport. Try this one line instead:

pane.setViewportView(container);

rebola at 2007-7-12 16:36:56 > top of Java-index,Desktop,Core GUI APIs...
# 2
Furthermore for absolute surity your panel will scroll correctly, it would have to implement the Scrollable interface. Try this class ScrollablePanel on for size. get it at: http://www.geocities.com/icewalker2g
icewalker2ga at 2007-7-12 16:36:56 > top of Java-index,Desktop,Core GUI APIs...
# 3

You don't need to implement the scrollable interface or anything, the panel will scroll automatically when the preferred size of the panel is greater than the size of the scroll pane.

The problem is that using a using a null layout manager so the panel doesn't have a preferred size. You are responsible for calculating the preferred size of the panel. That why we recommend you use a proper layout manager, it does all the work for you.

camickra at 2007-7-12 16:36:56 > top of Java-index,Desktop,Core GUI APIs...