Java Swing How to - Set JTextArea to Wrap by word








Question

We would like to know how to set JTextArea to Wrap by word.

Answer

/*www .ja  v a  2s  . c  o  m*/
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.text.AbstractDocument;

public class Main {
    public static void main(String[] args) {
        JFrame f = new JFrame();
        JTextArea ta = new JTextArea(5, 32);
        ta.setText("That's one small step for man...\nOne giant leap for mankind.");
        ta.setLineWrap(true);
        ta.setWrapStyleWord(true);

        f.getContentPane().add(ta);
        f.setSize(100, 100);
        f.setVisible(true);

    }
}