Open In App

Java JScrollPane

Last Updated : 13 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Java JScrollPane is a component in the Java Swing library that provides a scrollable view of another component, usually a JPanel or a JTextArea. it provides a scrolling functionality to the display for which the size changes dynamically. It is useful to display the content which exceeds the visible area of the window. In this article, we are going to see some constructors, methods, and examples of JScrollPane.

Constructor of JScrollPane

Constructors

Descriptions

JScrollPane()

It is a default constructor that creates an empty JScrollPane.

JScrollPane(Component comp)

This constructor creates a JScrollPane with the specified view component as the scrollable content.

JScrollPane(int vertical, int horizontal)

This constructor creates an empty JScrollPane with the specified vertical and horizontal scrollbar.

JScrollPane(LayoutManager layout)

This constructor creates a JScrollPane with the specified layout manager.

Methods of JScrollPane

Methods

Description

void setVerticalScrollBarPolicy(int vertical)

Sets the vertical scrollbar policy

void setHorizontalScrollBarPolicy(int horizontal)

Sets the horizontal scrollbar policy

void setColumnHeaderView(Component comp)

sets the column header for the JScrollPane

void setRowHeaderView(Component comp)

sets the rowheader for the JScrollPane

setCorner(String key, Component corner)

It is used to set a component to be displayed in one of the corners of the scroll pane

Component getCorner(String key)

It is used to retrieve the component that has been previously set in one of the corners of the scroll pane using the setCorner method

void setViewportView(Component comp)

It is used to set the component that will be displayed in the viewport of the scroll pane

Following are the programs to implement JScrollPane

1. Java Program to demonstrate simple JscrollPane

Below is the implementation of the above topic:

Java




// Java Program to demonstrate 
// Simple JscrollPane
import javax.swing.*;
import java.awt.*;
  
// Driver Class
public class SimpleJScrollPaneExample {
      // main function
    public static void main(String[] args) {
        
        JFrame frame = new JFrame("Simple JScrollPane Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        
        // Create a JPanel to hold a list of labels.
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        
        // Add a large number of labels to the panel.
        for (int i = 1; i <= 50; i++) {
            
            JLabel label = new JLabel("Label " + i);
            panel.add(label);
            
        }
        
        // Create a JScrollPane and set the panel as its viewport.
        JScrollPane scrollPane = new JScrollPane(panel);
        
        // Add the JScrollPane to the frame.
        frame.add(scrollPane);
        frame.setVisible(true);
    }
}


Output:

Output of JscollPane Example 1

2. Java Program to Implementing a JTextArea to JScrollPane

Below is the implementation of the above topic:

Java




// Java Program to Implementing 
// JTextArea to JScrollPane
import javax.swing.*;
  
// Driver Class
public class JTextAreaExample {
    // main function
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Java JTextArea in JScrollPane");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
  
        // Creating an JTextArea with 10 rows, 30 columns
        JTextArea textArea = new JTextArea(10, 30);
        
        // Creating an JScrollPane
        JScrollPane scrollPane = new JScrollPane(textArea);
  
        frame.add(scrollPane);
        frame.setVisible(true);
    }
}


Output:

Output of JScrollPane for Example 2



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads