TextArea in Java AWT


The TextArea control in AWT provide us multiline editor area. The user can type here as much as they wants. When the text in the text area become larger than the viewable area the scroll bar is automatically appears which help us to scroll the text up & down and right & left.

Constructor Used for
TextArea() new and empty text area with no text in it.
TextArea (int row, int column) new text area with specified number of rows and columns and empty string as text.
TextArea (String text) new text area and displays the specified text
TextArea (String text, int row, int column) new text area with the specified text in the text area and specified number of rows and columns.
TextArea (String text, int row, int column, int scrollbars) new text area with specified text in text area and specified number of rows and columns and visibility.

Source Code

package awtDemo;
 
import java.awt.*;
import java.awt.event.*;
 
//TextArea in Java AWT
class MyApp extends Frame implements ActionListener {
 
	TextArea t;
	Label l;
	TextField tf;
	Button b;
 
	public MyApp() {
		super("Tutor Joes");
		setSize(1000, 600);// w,h
		setLayout(null);
		setVisible(true);
 
 
		l=new Label("---");
		l.setBounds(20,50,250,30);
 
		t=new TextArea(10,30);//R C
		t.setBounds(20,100,300,200);
 
		tf=new TextField(20);
		tf.setBounds(20,350,300,30);
 
		b=new Button("Click");
		b.setBounds(20,400,100,30);
		b.addActionListener(this);
 
		add(l);
		add(t);
		add(tf);
		add(b);
		// Close Button Code
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				System.exit(0);
			}
		});
	}
 
	@Override
	public void actionPerformed(ActionEvent e) {
		//l.setText(t.getSelectedText());
		//t.append(tf.getText());
		t.insert(tf.getText(),t.getCaretPosition());
	}
 
}
 
public class app {
	public static void main(String[] args) {
		MyApp frm = new MyApp();
	}
 
}
 
To download raw file Click Here

Output

Java AWT


Basic Programs