Tutorialspoint

1 Answer
George John

To append text to JTextArea, use the component’s append() method. Let’sa say the following is our text in the JTextArea -

JTextArea textArea = new JTextArea("The text added here is just for demo. "+ "\nThis demonstrates the usage of JTextArea in Java. ");

Now, append text to the same textArea -

textArea.append("In this example we have deleted some text from the beginning."+ "\nWe have also appended some text.");

The following is an example to append text to JTextArea -

Example

package my;
import java.awt.GridLayout;
import javax.swing.*;
public class SwingDemo {
   SwingDemo() {
      JFrame frame = new JFrame("Demo");
      JTextArea textArea = new JTextArea("The text added here is just for demo. "+ "\nThis demonstrates the usage of JTextArea in Java. ");
      int begn = 0;
      int end = 10;
      textArea.replaceRange(null, begn, end);
      textArea.append("In this example we have deleted some text from the beginning."+ "\nWe have also appended some text.");
      frame.add(textArea);
      frame.setSize(550,300);
      frame.setLayout(new GridLayout(2, 2));
      frame.setVisible(true);
   }
   public static void main(String args[]) {
      new SwingDemo
      ();
   }
}

Output

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.