JTextArea with JScrollPane

A JTextArea component does not support scrolling directly, but it’s easy to add scrollbars by placing the JTextArea component in a JScrollPane container.

A ScrollPane is a component that presents a rectangular area in which component may be viewed. Horizontal and Vertical Scrollbars are provided if necessary. It can be created by ScrollPane Class.

Program 

fdsfd

 

 

 

Program Source

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.FlowLayout;

class Frame extends JFrame {
 
    Frame()
    {
        setTitle("JTextArea with JScrollPane");
        setLayout(new FlowLayout());
        setJTextArea();
        setSize(700,250);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 
    private void setJTextArea()
    {
        String data = "Java is a computer programming language that is n"
                     +"concurrent, class-based, object-oriented, and n"
                     +"specifically designed to have as few implementation n"
                     +"dependencies as possible. It is intended to let n"
                     +"application developers write once, run anywhere n"
                     +"(WORA), meaning that code that runs on one n"
                     +"platform does not need to be recompiled to run on another.nn"
                     +"Java applications are typically compiled to bytecode n"
                     +"(class file) that can run on any Java virtual machine n"
                     +"(JVM) regardless of computer architecture.";
        JTextArea jt = new JTextArea(data,8,20);
        JScrollPane js = new JScrollPane(jt,
                                         JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                         JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        add(js);
    }
}

public class Javaapp {
 
    public static void main(String[] args) {
     
       Frame fr = new Frame();
    }
}

Leave a Comment