Thursday, September 4, 2025
HomeLanguagesJavaJava Swing | JTextArea

Java Swing | JTextArea

JTextArea is a part of java Swing package . It represents a multi line area that displays text. It is used to edit the text . 
JTextArea inherits JComponent class. The text in JTextArea can be set to different available fonts and can be appended to new text . A text area can be customized to the need of user .
Constructors of JTextArea are: 

  1. JTextArea() : constructs a new blank text area .
     
  2. JTextArea(String s) : constructs a new text area with a given initial text.
     
  3. JTextArea(int row, int column) : constructs a new text area with a given number of rows and columns.
     
  4. JTextArea(String s, int row, int column) : constructs a new text area with a given number of rows and columns and a given initial text.
     

Commonly used methods : 

  1. append(String s) : appends the given string to the text of the text area.
     
  2. getLineCount() : get number of lines in the text of text area.
     
  3. setFont(Font f) : sets the font of text area to the given font. 
     
  4. setColumns(int c) : sets the number of columns of the text area to given integer.
     
  5. setRows(int r) : sets the number of rows of the text area to given integer.
     
  6. getColumns() : get the number of columns of text area.
     
  7. getRows() : get the number of rows of text area.
     

1. Program to create a simple JTextArea 
 

Java




// Java Program to create a simple JTextArea
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text extends JFrame implements ActionListener {
 
    // JFrame
    static JFrame f;
 
    // JButton
    static JButton b;
 
    // label to display text
    static JLabel l;
 
    // text area
    static JTextArea jt;
 
    // default constructor
    text()
    {
    }
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame to store text field and button
        f = new JFrame("textfield");
 
        // create a label to display text
        l = new JLabel("nothing entered");
 
        // create a new button
        b = new JButton("submit");
 
        // create a object of the text class
        text te = new text();
 
        // addActionListener to button
        b.addActionListener(te);
 
        // create a text area, specifying the rows and columns
        jt = new JTextArea(10, 10);
 
        JPanel p = new JPanel();
 
        // add the text area and button to panel
        p.add(jt);
        p.add(b);
        p.add(l);
 
        f.add(p);
        // set the size of frame
        f.setSize(300, 300);
 
        f.show();
    }
 
    // if the button is pressed
    public void actionPerformed(ActionEvent e)
    {
        String s = e.getActionCommand();
        if (s.equals("submit")) {
            // set the text of the label to the text of the field
            l.setText(jt.getText());
        }
    }
}


Output: 
 

2. Program to create a JTextArea and set a initial text and add buttons to change the font of text area. 
 

Java




// Java Program  to create a JTextArea and
// set a initial text and add buttons to change
// the font of text area.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text11 extends JFrame implements ActionListener {
 
    // JFrame
    static JFrame f;
 
    // JButton
    static JButton b, b1, b2, b3;
 
    // label to display text
    static JLabel l, l1;
 
    // text area
    static JTextArea jt;
 
    // default constructor
    text11()
    {
    }
 
    // main class
    public static void main(String[] args)
    {
        // create a new frame to store text field and button
        f = new JFrame("textfield");
 
        // create a label to display text
        l = new JLabel("nothing entered");
        l1 = new JLabel("0 lines");
 
        // create a new buttons
        b = new JButton("submit");
        b1 = new JButton("plain");
        b2 = new JButton("italic");
        b3 = new JButton("bold");
 
        // create a object of the text class
        text11 te = new text11();
 
        // addActionListener to button
        b.addActionListener(te);
        b1.addActionListener(te);
        b2.addActionListener(te);
        b3.addActionListener(te);
 
        // create a text area, specifying the rows and columns
        jt = new JTextArea("please write something ", 10, 10);
 
        JPanel p = new JPanel();
 
        // add the text area and button to panel
        p.add(jt);
        p.add(b);
        p.add(b1);
        p.add(b2);
        p.add(b3);
        p.add(l);
        p.add(l1);
 
        f.add(p);
        // set the size of frame
        f.setSize(300, 300);
 
        f.show();
    }
 
    // if the button is pressed
    public void actionPerformed(ActionEvent e)
    {
        String s = e.getActionCommand();
        if (s.equals("submit")) {
            // set the text of the label to the text of the field
            l.setText(jt.getText() + ", ");
            l1.setText(jt.getLineCount() + " lines");
        }
        else if (s.equals("bold")) {
 
            // set bold font
            Font f = new Font("Serif", Font.BOLD, 15);
            jt.setFont(f);
        }
        else if (s.equals("italic")) {
            // set italic font
            Font f = new Font("Serif", Font.ITALIC, 15);
            jt.setFont(f);
        }
        else if (s.equals("plain")) {
            // set plain font
            Font f = new Font("Serif", Font.PLAIN, 15);
            jt.setFont(f);
        }
    }
}


Output : 
 

 

 

Note : The following program might not run in an online compiler please use an offline IDE 
 

RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS