Introduction
A JscrollPane is used to create a scrollable component view. We use a scroll pane to display a large component or a component whose size can change dynamically when the screen size is constrained. Split panes and tabbed panes are two other containers for saving screen space.

When the screen size is limited, we must use the scroll pane for the following two scenarios:
1. To make a significant component large.
2. To display a changeable component with a dynamically changing size.
Viewports and scrollbars are combined in the JScrollPane class. It will link the scrollbar to our viewport. The scrollbar display policy properties allow us to control the appearance of our scrollbars: There are two types of scrollbar policies: verticalScrollbarPolicy and horizontalScrollbarPolicy.
JScrollPane offers a scrollable view of a component, which could be an image, table, tree, or other object. A JScrollPane can be attached to a component like JPanel or a top-level container like JFrame. Another lightweight component that extends the JComponent class is JScrollPane.

Constructors
| Constructor | Purpose |
JScrollPane() |
A scroll pane is created. When the Component argument is present, the scroll pane's client is set. The vertical and horizontal scroll bar policies are set by the two int parameters when they are present (respectively). |
JScrollPane(Component) | |
JScrollPane(int, int) | |
JScrollPane(Component, int, int) |
Useful Methods
| Modifier | Method | Description |
void |
setColumnHeaderView(Component) |
It determines the scroll pane's column header. |
void |
setRowHeaderView(Component) |
It determines the jscrollpane row header . |
void |
setCorner(String, Component) |
The specified corner is set or retrieved. The int parameter must be one of the ScrollPaneConstants : UPPER_LEFT_CORNER, UPPER_RIGHT_CORNER, LOWER_LEFT_CORNER, LOWER_RIGHT_CORNER, LOWER_LEADING_CORNER, LOWER_TRAILING_CORNER, UPPER_LEADING_CORNER, UPPER_TRAILING_CORNER. |
Component |
getCorner(String) |
|
void |
setViewportView(Component) |
It sets the scroll pane's client. |
Read more about java swing component
JScrollPane Example
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JtextArea;
public class ExampleJScrollPane {
private static final long serialversUID = 1 L;
private static void createdisplayGUI() {
// Create and set up the window.
final JFrame Frame = new JFrame("Scroll Pane Example");
// Display the window.
Frame.setSize(500, 500);
Frame.setVisible(true);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set flow layout for the frame
Frame.getContentPane().setLayout(new FlowLayout());
JTextArea textarea = new JTextArea(20, 20);
JScrollPane TextAreascrollable = new JScrollPane(textarea);
TextAreascrollable.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
TextAreascrollable.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Frame.getContentPane().add(TextAreascrollable);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createdisplayGUI();
}
});
}
}
Output:





