Updated July 1, 2023
Overview of JScrollPane in Java
JScrollPane is used to give a scrollable view to your component. When the screen size is small or limited, we can use a scroll pane to showcase a large component or a component whose size changes dynamically. The components like image, table, text, textarea, etc., should be lightweight. JScrollPane component should be inside the container like JFrame or JPanel. It is an important component in Graphic programming, especially your need to handle and display large data. In this topic, we are going to learn about JScrollPane in Java. When we have a limited screen size, then we need to use a scroll pane for the following two conditions:
- To display a large component.
- To display a dynamic size changeable component.
JScrollPane class is a combination of viewports and scrollbars. It will connect our viewport with the scrollbar. We can control our scrollbar’s appearances using scrollbar display policy properties: verticalScrollbarPolicy and horizontalScrollbarPolicy.
Both these properties can have values AS_NEEDED, ALWAYS, or NEVER. It also has two additional viewports:
- rowHeading – Used to scroll horizontally
- columnHeading – Used to scroll vertically
Constructors
The Sole purpose of the constructor of this class is to create a scroll pane. The input values undermine the dimensions of it these constructors, i.e., parameters. Constructors of JscrollPane class are of two types Parameterized and Non-Parameterized; they are further classified as below:
1. JScrollPane( )
Creates an empty scroll pane (no viewPort). It can have both vertical and horizontal scrollbars when needed.
Example:
import java.awt.*;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Jscrollpane {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JScrollPane scrollPane = new JScrollPane();
panel.add(scrollPane);
frame.setContentPane(panel);
frame.setSize(500, 500);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
Output:
2. JscrollPane (Component c)
Creates a scroll pane with the specified component. When the component content exceeds the view, horizontal and vertical scrollbars appear.
Example:
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Jscrollpane {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JTextArea tArea = new JTextArea(10,10);
JScrollPane scrollPane = new JScrollPane(tArea);
panel.add(scrollPane);
frame.setContentPane(panel);
frame.setSize(500, 500);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
Output:
3. JScrollPane(int vsPolicy, int hsPolicy)
Creates a scroll pane with the specified scroll policies.
Example:
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Jscrollpane {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JScrollPane scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
panel.add(scrollPane);
frame.setContentPane(panel);
frame.setSize(500, 500);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
Output:
4. JScrollPane(Component c, int vsPolicy, int hsPolicy)
Creates a scroll pane with the specified component. The component position is controlled with a pair of scrollbars.
Example:
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Jscrollpane {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JTextArea tArea = new JTextArea(10,10);
JScrollPane scrollPane = new JScrollPane(tArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
panel.add(scrollPane);
frame.setContentPane(panel);
frame.setSize(500, 500);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
Output:
Methods in JscrollPane Class
Below are the methods in JscrollPane Class.
- setColumnHeaderView(Component) – It sets the column header for the scroll pane of the specified component in the parameters.
- setRowHeaderView(Component) – It sets the row header for the scroll pane of the specified component in the parameters.
- setCorner(String key, Component) – It sets the corner for the scroll pane for the specified component in the parameter. The string key parameter is along with the following:
JScrollPane.UPPER_LEFT_CORNER, JScrollPane.UPPER_RIGHT_CORNER, JScrollPane.LOWER_LEFT_CORNER, JScrollPane.LOWER_RIGHT_CORNER, JScrollPane.LOWER_LEADING_CORNER, JScrollPane.LOWER_TRAILING_CORNER, JScrollPane.UPPER_LEADING_CORNER, JScrollPane.UPPER_TRAILING_CORNER - getCorner(Component) – It gets the corner for the scroll pane for the specified component in the parameter.
Examples of JScrollPane in Java
Here are some of the Examples given below.
1. Example Program for JscrollPane
Code:
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Jscrollpane {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JTextArea tArea = new JTextArea(20,20);
JLabel labelColumn = new JLabel("Column Header");
JLabel labelRow = new JLabel("Row Header");
JLabel label1 = new JLabel("UL");
JLabel label2 = new JLabel("UR");
JLabel label3 = new JLabel("LL");
JLabel label4 = new JLabel("LR");
JScrollPane scrollPane = new JScrollPane(tArea);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setColumnHeaderView(labelColumn);
scrollPane.setRowHeaderView(labelRow);
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER ,label1);
scrollPane.setCorner(JScrollPane.UPPER_RIGHT_CORNER ,label2);
scrollPane.setCorner(JScrollPane.LOWER_LEFT_CORNER ,label3);
scrollPane.setCorner(JScrollPane.LOWER_RIGHT_CORNER ,label4);
panel.add(scrollPane);
frame.setContentPane(panel);
frame.setSize(500, 500);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
Output:
2. Example of JTable with JScrollPane
Code:
import javax.swing.*;
import java.awt.*;
import javax.swing.table.TableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.DefaultTableCellRenderer;
public class JScrollDemo {
public static void main(String[] args) {
{
String[] index = new String[] { "S.No", "Continent", "Area (square.km)",
"Percentage Total Mass" };
JFrame frame = new JFrame("JScrollPane with JTable");
JLabel label = new JLabel("Continents Largest To Smallest", JLabel.CENTER);
Object[][] data = new Object[][] {
{ "S.No", "Continent", "Area (square.km)", "Percentage Total Mass" }
{ "1", "Asia", "44,579,000", "29.5%" },
{ "2", "Africa", "30,370,000", "20.4%" },
{ "3", "North America", "24,709,000", "16.5%" },
{ "4", "South America", "17,840,000", "12.0%" },
{ "5", "Antartica", "14,000,000", "9.2%" },
{ "6", "Europe", "10,180,000", "6.8%" },
{ "7", "Australia", "8,600,000", "5.9%" }, };
// creating a DeFaultTableModel object, which is subclass of
// TableModel
DefaultTableModel TableModel = new DefaultTableModel(data, index);
// Initializing a JTable from DefaultTableModel.
JTable table = new JTable(TableModel);
// Adjusting the contents of each cell of JTable in CENTER
DefaultTableCellRenderer tableCellRenderer = new DefaultTableCellRenderer();
// Aligning the table data centrally.
tableCellRenderer.setHorizontalAlignment(JLabel.CENTER);
table.setDefaultRenderer(Object.class, tableCellRenderer);
// Creating a JPanel, setting it layout to BorderLayout and adding
// JTable to it.
JPanel panel = new JPanel(new BorderLayout());
panel.add(table, BorderLayout.CENTER);
// Creating a JScrollPane and adding its functionalities to JPanel
JScrollPane scrollPane = new JScrollPane(panel);
// Adding a JLabel and JScrollPane to JFrame.
frame.add(label, BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(320, 200);
frame.setVisible(true);
}
}
}
Output:
Conclusion
When screen size is limited, we need to use a scroll pane to display large components or components whose size can change dynamically.
Recommended Articles
We hope that this EDUCBA information on “JScrollPane in Java” was beneficial to you. You can view EDUCBA’s recommended articles for more information.