Creating TextAreas in an Applet Window using Advanced Java Programming

Home  »  Java  »  Advance Java »    Java AWT (Abstract Window ToolKit) »    TextAreas

A multi-line text field is called text area box. A text field accepts is single line of input whereas a text area box can accept several lines of input. Java provides the AWT class, TextArea to create a textarea box. The constructors for creating a textarea box are:

  1. public TextArea(): is the empty constructor. It creates a text area box with the default setting for width the height.
  2. public TextArea(int height_in_chars, int width_in_chars): allows you to specify the height and width of the textarea box. The argument int heigh_in_chars specifies the height of the text area box in characters. The argument int width_in_chars specifies the width of the text area box in characters.
  3. public TextArea(String txt): allows you to specify the default text within the text area box. The height and width of the text area box are set to default size.
  4. public TextArea(String txt, int height_in_chars, int widht_in_chars): allows you to specify the default text, height and width of text area box.
  5. public TextArea(String txt, int height_in_chars, int width_in_chars, int scroll_value): allows you to specify the default text, height, width and scroll bars of the text area box.

The below table that show lists the scrollbar values for the textarea box that can be specified with the TextArea() constructor.

Values
Description
SCROLLBARS_BOTHDisplays both the horizontal and the vartical scrollbars with the text area box.
SCROLLBARS_HORIZONTAL_ONLYDisplays the horizontal scrollbar with the text area box
SCROLLBARS_NONEDisplays no scrollbars
SCROLLBARS_VERTICAL_ONLYDisplays the vertical scrollbar with the text area box.

You may use the TextArea constructors to create area boxes. The following program code show how to create text area using the various constructors available with the TextArea class.

Creating Text Area Boxes:

The above code will be stored in the file called TextAreaTest.java, which on compilation will crate a class file called TextAreaTest.class. Now compiple this file using below commands in the DOS mode:

C:\>jdk1.4\bin>javac TextAreaTest.java
C:\>jdk1.4\bin>appletviewer TextAreaTest.java



"Output of TextAreaTest.java"


Download Complete Program

  -  



another example:



The above code will be stored in the file called TextAreaTest2.java, which on compilation will crate a class file called TextAreaTest2.class. Now compiple this file using below commands in the DOS mode:

C:\>jdk1.4\bin>javac TextAreaTest2.java
C:\>jdk1.4\bin>appletviewer TextAreaTest2.java



"Output of TextAreaTest2.java"


Download Complete Program

Java Source Code   -   Class File



The below Table lists the methods that you can use with the TextArea class.
Methods
Description
String getText()Retrieves the text input in the text area box.
void setText(String txt)Sets the string value in the text area box to the value assigned to the argument, String txt
String getSelectedText()Retrieves the selected text from the text area box. The end user selects the text eigher by using the mouse or the keyboard
void select(int start, int end)Selects the text within the text area box starting from the position specified by the agrument, int start, to the position specified by the argument, int end.
boolean isEditable()Retrieves a value, true or false that tells whether the end user can modify the text within the text area box or not.
void setEditable(boolean true_false)Assigns the value, true or false that specifies the editable property of the text area box.
void append(String txt)Appends the text specified in the argument, String txt, to the end of the existing text in the text area box.
void insert(String txt, int index)Inserts a string specified by the argument, String txt, at the position specified by the argument, int index.
void replaceRange(String txt,int start, int end)Replaces the text starting from the postion specified by the argument, int start, to the position specified by int end with the text specified by String txt.




No comments:

Post a Comment