To append text to the end of the JTextArea
document we can use the append(String str)
method. This method does nothing if the document is null
or the string is null
or empty.
package org.kodejava.swing;
import javax.swing.*;
import java.awt.*;
public class TextAreaAppendText extends JPanel {
public TextAreaAppendText() {
initializeUI();
}
public static void showFrame() {
JPanel panel = new TextAreaAppendText();
panel.setOpaque(true);
JFrame frame = new JFrame("JTextArea Demo");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(TextAreaAppendText::showFrame);
}
private void initializeUI() {
String text = "The quick brown fox ";
JTextArea textArea = new JTextArea(text);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(textArea);
String appendText = "jumps over the lazy dog.";
textArea.append(appendText);
this.setPreferredSize(new Dimension(500, 200));
this.setLayout(new BorderLayout());
this.add(scrollPane, BorderLayout.CENTER);
}
}
The output of the code snippet above is:
Latest posts by Wayan (see all)
- How do I configure secure cookies using web.xml? - April 18, 2024
- How do I create a table with multiple header in iText 8? - March 16, 2024
- How do I add Image to a Table in iText 8? - March 11, 2024
How can I copy paste an image to a
JTextArea
in swing. Can you write a small code snippet?Thank you, it helped me to find my solution.