Create a Scroll Pane Container in Java

In this section, you will learn how to create a scroll pane container in Java Swing.

Create a Scroll Pane Container in Java

In this section, you will learn how to create a scroll pane container in Java Swing.

Create a Scroll Pane Container in Java

Create a Scroll Pane Container in Java

     

In this section, you will learn how to create a scroll pane container in Java Swing. When you simply create a Text Area and putting text in the that then the size of the text area is increased according to the length of the text. But you can fix the size of the text area through adding the JScrollPane component of Java Swing. Following is the screen shot for the result of the given program:

Swing Scroll Bar Component with Text Area

This program shows a scroll pane for the given text area. The following methods and APIs used in the program for creating the JScrollPane container.

JScrollPane(Component view): 
This is the constructor of JScrollPane Class. It extends from the JComponent. This constructor uses to create a JScrollPane . This JScrollPane displays the horizontal and vertical scroll bar. It takes the components for which the scroll pane has to be set.

Here is the code of program:

import javax.swing.*;

public class JScrollContainer{
public static void main(String[] args) {
String st = "Math_calculs_coordinate\n" 
"Computer_Operator_Programer\n" 
"Physics_Laboratry\n"
"Chemestru\n" 
"Biology" 
"Civics\n" +"History" 
"Hindi\n" "English\n" 
"Tally" "Coordinate\n"
"Trigonometry" "ram is a good boy\n" 
"He has four cars and two computers.";
JFrame frame = new JFrame("Creating a JScrollPane Container");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JTextArea area = new JTextArea(st,5,6);
JScrollPane spane = new JScrollPane(area);
panel.add(spane);
frame.add(panel);
frame.setSize(400,400);
frame.setVisible(true);
  }
}

Download this example