import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
public class JScrollPaneDemo extends JFrame {
public JScrollPaneDemo(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(20,20,300,200);
initControls();
this.setVisible(true);
}
private void initControls(){
JPanel p=new JPanel();
p.setLayout(new BorderLayout());
JPanel p1=new JPanel();
p1.setLayout(new GridLayout(20, 20));
JButton []b=new JButton[20];
int b1 = 0;
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
p1.add(new JButton("Button " + b1));
++b1;
}
}
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(p1, v, h);
p.add(jsp,BorderLayout.CENTER);
this.add(p);
}
/**
* @param args
*/
public static void main(String[] args) {
new JScrollPaneDemo();
}
}