Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
AWT TextArea
2.1.
Syntax
3.
Fields of TextArea Class
4.
TextArea Constructors
5.
TextArea Methods
6.
Examples
6.1.
Code 1
6.2.
Code 2
7.
Frequently Asked Questions
7.1.
What is Java AWT?
7.2.
What is the textarea in Java AWT?
7.3.
How to scroll text in AWT textarea?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

AWT TextArea

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

In AWT, the TextArea control provides a multiline editing area. The user can enter as much as he likes in this box. When the text in the text area grows more significant than the viewing area, a scroll bar emerges, allowing us to scroll the text up and down and right and left. We shall be learning about more of this in the upcoming section of this blog.

Read More About, Basics of Java

AWT TextArea

The TextArea class constructs a multiline text area that allows users to type on numerous lines. TextArea is a small component that extends the TextComponent class, extending the JComponent class.

A multiline zone that displays text is the object of the TextArea class. It allows you to alter numerous lines of text. It is derived from the TextComponent class.

We may input as much text as we wish in the text field.

The scroll bar emerges automatically when the text in the text field grows more remarkable than the viewing area, allowing us to scroll the text up and down or right and left.

Syntax

public class TextArea extends TextComponent
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Fields of TextArea Class

The following are the fields of java.awt.TextAreas class.

  • static int SCROLLBARS_BOTH: It helps us create horizontal and vertical scroll bars.
     
  • static int SCROLLBARS_HORIZONTAL_ONLY: It allows us to create horizontal scrollbars and displays them.
     
  • static int SCROLLBARS_VERTICAL_ONLY: It not only helps us to create vertical scrollbars but also displays them.
     
  • static int SCROLLBARS_NONE: It doesn't display any scrollbar in the textarea.

TextArea Constructors

Let's learn about few constructors of the TextArea object.

TextArea Methods

Now, let's see some methods that we can use in TextArea.

You can also read about the Multiple Inheritance in Java.

Examples

Let's see a few examples related to AWT Text Area.

Code 1

import java.awt.Color;
import java.awt.Frame;
import java.awt.TextField;
public class TextAreaExample {


	public static void main(String[] args) {

	Frame f = new Frame();

	f.setVisible(true);

	f.setSize(200,200);

	TextField tf = new TextField("Hello Coding Ninjas");

	f.add(tf);

	}
}

Output

Demo Output of Code

In this example, we have created a Text Area with the mentioned size and passed some text printed on the text area.
 

Now let us discuss another example but with an Action Listener. Action Listener is used to detecting particular events in the window frame. Try it on java online compiler.

Code 2

import java.awt.*;    
import java.awt.event.*;    

public class TextAreaDemo extends Frame implements ActionListener {

	Label l1, l2;    
	TextArea area;    
	Button b;    
  	
	TextAreaDemo() {    


		l1 = new Label();    
		l1.setBounds(50, 50, 100, 30);    
		l2 = new Label();    
		l2.setBounds(160, 50, 100, 30);    
		area = new TextArea();    
		area.setBounds(20, 100, 300, 300);    
		b = new Button("Count Words");    
		b.setBounds(100, 400, 100, 30);    
  		
 		
		b.addActionListener(this);    
  		
 		
		add(l1);  
		add(l2);  
		add(area);  
		add(b);    


		setSize(400, 450);    
		setLayout(null);    
		setVisible(true);    
	}    

	public void actionPerformed(ActionEvent e) {    
		String text = area.getText();    
		String words[]=text.split("\\s");    
		l1.setText("Words: "+words.length);    
		l2.setText("Characters: "+text.length());    
	}    
	// main method  
	public static void main(String[] args) {    
		new TextAreaDemo();    
	}    
}

Output

In this example, we have created a text area with some specialty added to the text area. It counts the number of text and characters written in the text area.

Frequently Asked Questions

What is Java AWT?

Java AWT (Abstract Window Toolkit) is a Java API for creating graphical user interface (GUI) or window-based applications. It is Platform-dependent on which Java AWT components are shown in accordance with the operating system's perspective.

What is the textarea in Java AWT?

TextArea class object is a multiline zone that displays text. It allows you to alter numerous lines of text. It is derived from the TextComponent class.

How to scroll text in AWT textarea?

In AWT, the TextArea control provides us with a multiline editing area. The user can enter as much as he likes in this box. A scroll bar emerges when the text in the text area grows more significant than the viewing area, allowing us to scroll the text up and down and right and left.

Conclusion

In this article, we have discussed the AWT Text Area. We have introduced the topic along with the field of text area class. We also discussed the constructors, methods, and several code examples and explanations.

If you want to explore more about AWT, visit here.

You can improve your skills in  Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and more with our Coding Ninjas Studio  Guided Path. If you want to sharpen your coding skills to the test, check out the mock test series and enter the contests on Coding Ninjas Studio! If you're just getting started to know what questions big giants like Amazon, Microsoft, and Uber ask, check the problemsinterview experiences, and interview bundle for placement preparations.

We hope that this blog has helped you in enhancing your knowledge. 

"Happy Coding!".

Previous article
AWT TextField
Next article
AWT CheckBox
Live masterclass