Modifying Text in a JTextArea Component - Java Swing

Java examples for Swing:JTextArea

Description

Modifying Text in a JTextArea Component

Demo Code

import javax.swing.JTextArea;

public class Main {
  public static void main(String[] args) {
    // Create the text area
    JTextArea ta = new JTextArea("Initial Text");

    // Insert some text at the beginning
    int pos = 0;/*from   w  w  w .j  a v  a2s. c om*/
    ta.insert("some text", pos);

    // Insert some text after the 5th character
    pos = 5;
    ta.insert("some text", pos);

    // Append some text
    ta.append("some text");

    // Replace the first 3 characters with some text
    int start = 0;
    int end = 3;
    ta.replaceRange("new text", start, end);

    // Delete the first 5 characters
    start = 0;
    end = 5;
    ta.replaceRange(null, start, end);
  }
}

Related Tutorials