TEXTAREA Control Of AWT – EXAMPLE

About TextArea Example

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:

AWT TextArea Control
AWT 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:

About TextArea Example
About TextArea 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:

Next, we create AWT Panel Container with TextField and Button control in it. Then we create an AWT TextArea. Below is the code:

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:

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.

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.

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:

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:

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:

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:

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.

6. Complete Code Example

6.1 MainEntryAWT.java

6.2 FrameWindow.java

7. Watch AWT TextArea Control Example on YouTube

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.