Java JScrollPane create

Description

Java JScrollPane create

// Demonstrate JScrollPane. 
import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

class Demo extends JPanel {

   public Demo() {
      // Add 400 buttons to a panel.
      JPanel jp = new JPanel();
      jp.setLayout(new GridLayout(20, 20));
      int b = 0;// www  . ja v  a  2s  .c  om
      for (int i = 0; i < 20; i++) {
         for (int j = 0; j < 20; j++) {
            jp.add(new JButton("Button " + b));
            ++b;
         }
      }

      // Create the scroll pane.
      JScrollPane jsp = new JScrollPane(jp);

      add(jsp, BorderLayout.CENTER);
   }
}

public class Main {
   public static void main(String[] args) {
      Demo panel = new Demo();

      JFrame application = new JFrame();

      application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      application.add(panel);
      application.setSize(250, 250);
      application.setVisible(true);
   }
}



PreviousNext

Related