Showing posts with label how to create text area in awt. Show all posts
Showing posts with label how to create text area in awt. Show all posts

Tuesday, 19 February 2019

Text Area in java AWT | Java AWT Text Area control - Online Help

TextArea in java AWT:

The text area in java awt is a class which allows us to enter multiple lines of the text. The user can type a number of lines .if the text exceeds the text area, then scroll will be added automatically so that the user can easily scroll the text up and down, and left and right side of the text area.

Constructors available in java awt TextArea are:


The text area has 5 constructors. They are :

TextArea()
It is used to create an Empty text area.

TextArea(String str)
It is used to create a text area with the specified string.

TextArea(int rowsize,int colsize)
It is used to create an Empty text area with the specified number of rows and columns.

TextArea(String str,int rowsize,int colsize)
It is used to create a text area with the specified string and the specified number of rows and columns.

TextArea(String str, int rowsize,int colsize,int scrollbars)
It is used to create a text area with the specified string and specified number and columns with scrollbar.it are possible values are:

SCROLLBARS.BOTH
       It displays both horizontal and vertical scroll bars

SCROLLBARS. HORIZONTAL ONLY
      It displays the only horizontal scroll bar

SCROLLBARS.VERTICAL ONLY:
      It displays the only vertical scrollbar


Methods available in java awt textarea control:

The most commonly used TextArea() methods are as follow:

void addTextListener(TextListener t1) 
This method is used to configure an event handler for the text area.

String getText()
The getText() method is used to return the text in the field.

void setText(String str)
The setText() method is used to set the text for the field.

void append(String str)
This method is used to add text to the end of the component.

void setEditable(Boolean state)
This method is used to set the field as being editable or fixed.

void setFont(Font strfont)
This method is used to set the font for the text area.

void setBackground(Color bgcolor)
The setBackground() method is used to set the background color of the text area.

Sample program:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/* <applet code="areademo" width=500 height=500>
</applet> */
public class areademo extends Applet implements ActionListener
{
TextArea ta1;
Button b1;
public void init()
{
ta1=new TextArea(" ",10,20,TextArea.SCROLLBARS_BOTH);
add(ta1);
b1=new Button("click");
add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String msg="welcome to my blog";
if(e.getSource()==b1);
{
ta1.insert(msg,0);
}
}
}