Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I append text to JTextArea in Java?
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