Java Swing How to - Insert large text to JTextPane








Question

We would like to know how to insert large text to JTextPane.

Answer

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/*w w  w.j a va2s  .c o  m*/
public class Main {

  public static void main(String argv[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String testStr = "Paste text here.";
    JTextArea wrapArea = new JTextArea(testStr, 20, 40);
    wrapArea.setLineWrap(true);
    wrapArea.setWrapStyleWord(true);
    wrapArea.setCaretPosition(testStr.length());
    frame.add(new JScrollPane(wrapArea));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}