Hi,
set the setPreferredSize of the JPanel and add it to the scrollpane and then add the scroll pane to the JFrame,
u will get the scrollbar here is a code example which u may look at
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestRepaint extends JFrame
{
public TestRepaint()
{
super("layered pane");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane(new MyPanel());
Container contentPane = this.getContentPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
setSize(new Dimension(800,600));
this.setVisible(true);
}
class MyPanel extends JPanel
{
public MyPanel()
{
setPreferredSize(new Dimension(2000,600) );
this.setBackground(Color.white);
this.setOpaque(true);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
System.out.println ("Painting Back " );
g.setColor(Color.blue);
int x = 0;
for(int i = 0; i < 60; i++)
{
x +=60;
g.drawLine(x, 0, x, 600);
}
}
}
public static void main(
String args[])
{
new TestRepaint();
}
}