[JavaSwing] JTextArea in Java

JTextArea is a component for displaying multiple lines of text at the same time the user can edit the text.

Create a simple JTextArea

Now we will practice even an example that allows you to type and edit text. In your code, and images were explained quite clearly the commands performed with JTextArea.
JTextArea in java

code by nguyenvanquan7826 - DemoJTextArea.java
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package nguyenvanquan7826.JTextArea;
  
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
  
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
  
/**
 * ----------------- @author nguyenvanquan7826 -----------------
 * ---------------nguyenvanquan7826.wordpress.com --------------
 */
public class DemoJTextArea extends JFrame {
  
    private JTextArea ta;
  
    public DemoJTextArea() {
  
        add(createMainPanel());
  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Demo JTextArea");
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
  
    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new BorderLayout());
  
        // JScrollPane create a scroll when row of text larger than row of
        // JTextArea
        JScrollPane scroll = new JScrollPane(ta = createTextArea(10, 40));
        panel.add(scroll, BorderLayout.CENTER);
  
        JButton btnClear = new JButton("Clear");
        btnClear.addActionListener(new ActionListener() {
  
            @Override
            public void actionPerformed(ActionEvent e) {
                clear();
            }
        });
  
        JPanel panelBottom = new JPanel();
        panelBottom.add(btnClear);
  
        panel.add(panelBottom, BorderLayout.PAGE_END);
  
        return panel;
    }
  
    /**
     * create a JTextArea with rows and columns, two method setWrapStyleWord and
     * setLineWrap make text can down line when text too long
     */
    private JTextArea createTextArea(int row, int col) {
        JTextArea ta = new JTextArea(row, col);
        ta.setWrapStyleWord(true);
        ta.setLineWrap(true);
        return ta;
    }
  
    /**
     * clear text of JTextArea
     */
    private void clear() {
        ta.setText("");
    }
  
    public static void main(String[] args) {
        new DemoJTextArea();
    }
}

Set the font and font color for JTextArea

You can set the font and font color like the following function:

code by nguyenvanquan7826 - DemoJTextArea.java
1
2
3
4
5
6
7
8
/**
 * change font and color text of JTextArea
 */
private void changeColor() {
    Font font = new Font("Verdana", Font.BOLD, 20);
    ta.setFont(font);
    ta.setForeground(Color.BLUE);
}

font and color in JtextArea

Read more : TUT Java swing, class JTextArea, use JTextArea