Set word wrap style to word for JTextArea in Java
Description
The following code shows how to set word wrap style to word for JTextArea.
Example
import java.awt.Dimension;
//from w ww. ja v a 2s. c om
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Main {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea area = new JTextArea();
area.setLineWrap(true);
area.setWrapStyleWord(true);
area.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
f.add(new JScrollPane(area));
f.setSize(new Dimension(350, 300));
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »