1. About TextArea Control
The Java AWT TextField will allow one line of text. AWT TextArea Control is the solution for this. With this Control, we can decide the control size based on rows and columns. A column denotes a single character width. A row defines a complete line of text. For example, 10 row x 60 Column text area allows 60 characters of text in a single row and the height of the control is adjusted to display 10 lines of texts. When the column or row size goes beyond this limit, the TextArea Control displays scroll bars.
Below is a sample TextArea Control:

The Control here shows three lines of text which is not viable for the TextField Control. The control also sets up the vertical or horizontal scroll bars when needed. In this example, we will use the TextArea control and perform basic operations in it.
2. TextArea Control Example
Now have a look at the below picture and this is the sample UI we will create in this Example:

The item marked as 1 is the AWT TextArea control. We will add this control to the center of AWT Frame and you can see it occupies 80% of the space. Towards the bottom we add three more AWT Controls. First one is the TextField (Marked as 2) and after that we add an AWT Button (Marked as 3). Finally, we add a Checkbox (Marked as 4) and we use this check box to insert text in the Text Area control.
We can type a line of text in the TextField and click on the Add Text button. This will add a single line of text in the AWT TextArea Control. Each time we click Add Text button, a new line of text is added to the TextArea Component. When the check box not checked, we are in text selection mode. User can move their mouse and select any text and delete it or add text manually by changing the cursor position through the mouse.
When the Check box is in checked state, we are in Text-Insert mode. We can type a word or line of text in the text field and then click the mouse cursor at a different location on the TextArea Control to insert it. This way we can insert same text at different location quickly.
3. Add TextArea & Other Controls
Our example requires interacting with TextField and TextArea when the user clicks the Add Text button. So, we declared these two AWT components as class members. Below is the code:
1 2 3 |
//Sample01: To implement Text Area Control TextArea ta; TextField txtInput; |
Next, we create AWT Panel Container with TextField and Button control in it. Then we create an AWT TextArea. Below is the code:
1 2 3 4 5 6 7 8 |
//Sample 02: Create & Add Controls Panel ContolPanel = new Panel(); ContolPanel.setLayout(new FlowLayout()); txtInput = new TextField(30); Button btnAdd = new Button("Add Text"); ContolPanel.add(txtInput); ContolPanel.add(btnAdd); ta = new TextArea(); |
4. Append Line of Text to TextArea
In this section, we will see how to add text to the TextArea Control at runtime. As already explained in section 2, we need to add text when the user clicks the AddText button. Here, we implement ActionListener
for our Frame:
1 2 3 4 |
//Sample 03: Add ActionListener public class FrameWindow extends Frame implements WindowListener, ActionListener { |
The Button control in our Frame Window signs up with the ActionListener
using addActionListener
API and sets an action command, add. In the event handler, we can know the action source with this action command.
1 2 3 |
//Sample 04: Register the Button to Listener btnAdd.addActionListener(this); btnAdd.setActionCommand("Add"); |
In the actionPerformed
handler method, we get the click source by calling the getActionCommand
method. In our example, we have only one button. When there are multiple buttons, this API call is useful. We can use
append method of the AWT TextArea Control to add text to it. In our example, we first get string from the text box via getText
and pass that string to the
append method. Note how we add \n
to append text in a next line of TextArea Control.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@Override public void actionPerformed(ActionEvent e) { //Sample 05: Add String to Text Area String cmd = e.getActionCommand(); String InputText; switch (cmd) { case "Add": InputText = txtInput.getText() + "\n"; ta.append(InputText); break; default: break; } } |
5. Insert Text to TextArea Control at Mouse Click Location
In this section, we will explore how to insert text into the TextArea. Here, we will specify the insertion location using the mouse cursor. Our Frame class will implement MouseListener
for tracking the mouse click. Below is the code:
1 2 3 |
//Sample 06: Add MouseListener public class FrameWindow extends Frame implements WindowListener, ActionListener, MouseListener |
Our example will insert the text at mouse click location in the TextArea Control only when the ‘Left Click to Insert’ checkbox is ticked. We have not yet created this checkbox. First, we will declare it as a class member:
1 2 |
//Sample 07: Check Box to Insert Text Checkbox chkInsert; |
After declaring the Checkbox
, we create it and add it to the Panel
container. Note, this panel already has a TextField
and AWT Button
. The BorderLayout manages our Frame and we add TextArea to the Centre and Panel Container to the South. Below is the code:
1 2 3 4 5 |
//Sample 08: Create a Check Box chkInsert = new Checkbox("Left Click to Insert"); ContolPanel.add(chkInsert); add(BorderLayout.CENTER, ta); add(BorderLayout.SOUTH, ContolPanel); |
The TextArea control must respond to the Mouse Click and insert the text at mouse cursor location. So we should register it with the MouseListener
. Below is the code:
1 2 |
//Sample 09: Register TextArea with MouseListener ta.addMouseListener(this); |
The
insert function of the AWT TextArea Control punches the string to the specified location. We can use this function to plug the text anywhere in the TextArea. In the below code, first we check left mouse button is clicked by matching the getButton
return value with the MouseEvent
constant BUTTON1
. Note, we also make sure the check box is in ticked state before proceeding with the insertion.
From the getText
method, we fetch the text to be inserted from the text box and from
getCaretPosition method of the TextArea Control, we get current caret location picked by the user via mouse click. We pass this two data to the TextArea control to insert the text in the current caret position. Once the text to be inserted is chosen, we can plug it into multiple location within the Control.
1 2 3 4 5 6 7 8 9 10 11 |
//Sample 10: Insert Text into TextArea public void mouseClicked(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1 && chkInsert.getState() == true) { String InputText = txtInput.getText(); int Pos = ta.getCaretPosition(); ta.insert(InputText, Pos); } } |
6. Complete Code Example
6.1 MainEntryAWT.java
1 2 3 4 5 6 7 8 9 10 11 |
package AwtDemoPkg; import java.awt.Frame; public class MainEntryAwt { public static void main(String[] args) { //Sample 03: Make the FrameWindow Visible FrameWindow fw = new FrameWindow("AWT TextArea Example"); fw.setVisible(true); } } |
6.2 FrameWindow.java
1 2 3 4 5 6 7 8 9 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
package AwtDemoPkg; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Checkbox; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Panel; import java.awt.TextArea; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //Sample 03: Add ActionListener //Sample 06: Add MouseListener public class FrameWindow extends Frame implements WindowListener, ActionListener, MouseListener { //Sample01: To implement Text Area Control TextArea ta; TextField txtInput; //Sample 07: Check Box to Insert Text Checkbox chkInsert; public FrameWindow(String FrameTitle) { //Display the Frame Window super(FrameTitle); setSize(500, 200); setLocation(100,100); addWindowListener(this); //Sample 02: Create & Add Controls Panel ContolPanel = new Panel(); ContolPanel.setLayout(new FlowLayout()); txtInput = new TextField(30); Button btnAdd = new Button("Add Text"); ContolPanel.add(txtInput); ContolPanel.add(btnAdd); ta = new TextArea(); //Sample 04: Register the Button to Listener btnAdd.addActionListener(this); btnAdd.setActionCommand("Add"); //Sample 08: Create a Check Box chkInsert = new Checkbox("Left Click to Insert"); ContolPanel.add(chkInsert); add(BorderLayout.CENTER, ta); add(BorderLayout.SOUTH, ContolPanel); //Sample 09: Register TextArea with MouseListener ta.addMouseListener(this); } public void windowOpened(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { this.dispose(); } @Override public void actionPerformed(ActionEvent e) { //Sample 05: Add String to Text Area String cmd = e.getActionCommand(); String InputText; switch (cmd) { case "Add": InputText = txtInput.getText() + "\r\n"; ta.append(InputText); break; default: break; } } //Sample 10: Insert Text into TextArea public void mouseClicked(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1 && chkInsert.getState() == true) { String InputText = txtInput.getText(); int Pos = ta.getCaretPosition(); ta.insert(InputText, Pos); } } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} } |
7. Watch AWT TextArea Control Example on YouTube
Categories: AWT
Tags: append, getCaretPosition, getText, insert