Tuesday, July 23, 2013

TextArea

TextArea class allows you to create a text area in which you can input and edit multiple lines of text. TextArea is able to generate text and key events as the TextField does. Its constructors and useful methods are summarized below.

Constructors of TextArea class:
-TextArea()
creates an empty text area.
-TextArea(int rows, int columns)
creates an empty text area with specifying numbers of rows and columns.
-TextArea(String text, int rows, int columns)
create a text area with default text and specifying numbers of rows and columns.

Useful methods of TextArea class:

-append(String text):void
appends text to the text area.
-getText()
reads text from the text area.
-getSelectedText()
reads selected text from the text area.
-insert(String text, int pos)
inserts text to the text area at the specified position.
-replaceRange(String text, int start,int end)
replaces a rangle of text from start to end positions with the new text specified by text argument.
-select(int start,int end)
selects text in the text area from start to end positions.
-setText(String text)
places text to the text area. The old text will be replaced.

In the example code below, we create a TextArea with 10 rows and 50 columns. To make it bigger you need to increases its row and column values. The scroll bars are automatically attached to text area when the input text reaches the rows or columns limit.

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class InterfaceShow extends Frame{
InterfaceShow(String title){
setTitle(title);
setLayout(new FlowLayout());
setSize(new Dimension(500,300));

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

TextArea txtarea=new TextArea(10,50);
add(txtarea);

setVisible(true);
validate();
}
}
public class AWTControls {
public static void main(String[] args){
new InterfaceShow("Using TextArea");

}
}


textarea awt

No comments:

Post a Comment