Set line wrap for JTextArea in Java
Description
The following code shows how to set line wrap for JTextArea.
Example
import java.awt.Dimension;
import java.awt.FlowLayout;
/* w ww. j av a 2 s. c o m*/
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);
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 »