javacodex.com
Java Examples
Java Examples
memu home questions

JTextArea

A JTextArea can handle multiline plain text. Most often, when you have multiline text in a JTextArea, you will need scrolling capabilities.

This is an example of an application using a java swing JTextArea. It demonstrates how to read, write and append to the text area.

Source: (JTextAreaExample.java)

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
 
public class JTextAreaExample extends JFrame {
 
  public JTextAreaExample() {
 
    JTextArea textArea = new JTextArea(10,25);
 
    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());
    panel.add(textArea);
 
    this.setContentPane(panel);
    this.setTitle("JTextAreaExample Example");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.pack();
 
    // Wite text to the text area.
    textArea.setText("This is a string of text.\n");
 
    // Appends the given text to the end of text area.
    textArea.append("Another line of text");
 
    // read contents of the text area.
    String str = textArea.getText();
 
  }
 
  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JTextAreaExample();
    frame.setVisible(true);
  }
 
}
 

Output:

$ java JTextAreaExample

Output



Contact: javacodex@yahoo.com