Java Swing | JTextArea Last Updated : 10 May, 2022 Comments Improve Suggest changes Like Article Like Report 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: JTextArea() : constructs a new blank text area . JTextArea(String s) : constructs a new text area with a given initial text. JTextArea(int row, int column) : constructs a new text area with a given number of rows and columns. 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 : append(String s) : appends the given string to the text of the text area. getLineCount() : get number of lines in the text of text area. setFont(Font f) : sets the font of text area to the given font. setColumns(int c) : sets the number of columns of the text area to given integer. setRows(int r) : sets the number of rows of the text area to given integer. getColumns() : get the number of columns of text area. 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 Comment More infoAdvertise with us Next Article Java Swing | JPasswordField A andrew1234 Follow Improve Article Tags : Misc Java Java Programs Programming Language java-swing +1 More Practice Tags : JavaMisc Similar Reads Java Swing | JPasswordField PasswordField is a part of javax.swing package . The class JPasswordField is a component that allows editing of a single line of text where the view indicates that something was typed by does not show the actual characters. JPasswordField inherits the JTextField class in javax.swing package.Construc 5 min read Java Swing | Simple User Registration Form Swing is a part of the JFC (Java Foundation Classes). Building Graphical User Interface in Java requires the use of Swings. Swing Framework contains a large set of components which allow a high level of customization and provide rich functionalities, and is used to create window-based applications. 5 min read Java Swing | Simple User Registration Form Swing is a part of the JFC (Java Foundation Classes). Building Graphical User Interface in Java requires the use of Swings. Swing Framework contains a large set of components which allow a high level of customization and provide rich functionalities, and is used to create window-based applications. 5 min read Java Swing | Create a simple text editor To create a simple text editor in Java Swing we will use a JTextArea, a JMenuBar and add JMenu to it and we will add JMenuItems. All the menu items will have actionListener to detect any action.There will be a menu bar and it will contain two menus and a button:Â File menuopen: this menuitem is used 6 min read Java Swing | Create a simple text editor To create a simple text editor in Java Swing we will use a JTextArea, a JMenuBar and add JMenu to it and we will add JMenuItems. All the menu items will have actionListener to detect any action.There will be a menu bar and it will contain two menus and a button:Â File menuopen: this menuitem is used 6 min read Java Swing | Creating Custom Message Dialogs Though Java Swing provides built-in message dialog to display messages, we can create custom message dialog by using JWindow and other Java Swing elements. The advantage of creating them is that they are highly customizable and we can add the desired look-and-feel and functionalities to them.In this 4 min read Like