Java Swing How to - Replace the first 3 characters in JTextArea








Question

We would like to know how to replace the first 3 characters in JTextArea.

Answer

/*from ww  w.  j ava 2  s .  co m*/

import javax.swing.JTextArea;

public class Main {
  public static void main(String[] argv) {
    JTextArea ta = new JTextArea("Initial Text");
    int start = 0;
    int end = 3;
    ta.replaceRange("new text", start, end);
  }
}