Modifying Text in a JTextArea Component

The text in a text area can be modified using the method described in Modifying Text in a JTextComponent. However, the text area supports a similar set of convenience editing methods that are demonstrated in this example.
// Create the text area
JTextArea ta = new JTextArea("Initial Text");

// Insert some text at the beginning
int pos = 0;
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);

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.