Editing text in a JTextArea



Introduction

This example shows how you can edit the text inside the JTextArea.

// 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);

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.