Add JTextArea to JScrollPane - Java Swing

Java examples for Swing:JTextArea

Introduction

The following code shows how to add JTextArea to JScrollPane.

Demo Code

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Main {

  public static void main(String[] args) {
    // Create JTextArea
    JTextArea resumeTextArea = new JTextArea("Enter resume here", 10, 50);

    // Add JTextArea to a JScrollPane
    JScrollPane sp = new JScrollPane(resumeTextArea);

    JFrame myFrame = new JFrame();
    // Get the reference of the content pane of the JFrame
    Container contentPane = myFrame.getContentPane();

    // Add the JScrollPane (sp) to the content pane, not the JTextArea
    contentPane.add(sp, BorderLayout.CENTER);
  }/*  w ww.  jav  a2s . c  om*/
}

Related Tutorials