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.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »