Create a JTextArea and add to JScrollPane in Java

Description

The following code shows how to create a JTextArea and add to JScrollPane.

Example


import java.awt.Dimension;
import java.awt.FlowLayout;
//from w w  w .j  av  a2  s. c  om
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("JTextArea Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
    JTextArea textAreal = new JTextArea("from java2s.com", 5, 10);
    textAreal.setPreferredSize(new Dimension(100, 100));
    
    JScrollPane scrollPane = new JScrollPane(textAreal, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    textAreal.setLineWrap(true);

    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
  }
}

The code above generates the following result.

Create a JTextArea and add to JScrollPane in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer