Search code examples
javaswingoffsetjtextareasettext

JTextArea - How to set text at a specified offset?


I want to set some text at a specified offset in my JTextArea. Let's say I have already in my edit "aaa bbb" and I want to overwrite "bbb" with "house", how can I do that in Java?


Solution

  • You could use replaceRange()

    public void replaceRange(String str, int start, int end)

    Replaces text from the indicated start to end position with the new text specified. Does nothing if the model is null. Simply does a delete if the new string is null or empty.

    This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.