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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 diaplay 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 stor 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());
        }
    }
}

chevron_right


Output:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program 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 diaplay 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 stor 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);
        }
    }
}

chevron_right


Output :


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



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.