Chapter: Java The Complete Reference - The Java Library - Using AWT Controls, Layout Managers, and Menus

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Using a TextArea - AWT Controls

Sometimes a single line of text input is not enough for a given task. To handle these situations, the AWT includes a simple multiline editor called TextArea.

Using a TextArea

 

Sometimes a single line of text input is not enough for a given task. To handle these situations, the AWT includes a simple multiline editor called TextArea. Following are the constructors for TextArea:

 

TextArea( ) throws HeadlessException

 

TextArea(int numLines, int numChars) throws HeadlessException TextArea(String str) throws HeadlessException

TextArea(String str, int numLines, int numChars) throws HeadlessException 

TextArea(String str, int numLines, int numChars, int sBars) throws HeadlessException

 

Here, numLines specifies the height, in lines, of the text area, and numChars specifies its width, in characters. Initial text can be specified by str. In the fifth form, you can specify the scroll bars that you want the control to have. sBars must be one of these values:

 


TextArea is a subclass of TextComponent. Therefore, it supports the getText( ), setText( ), getSelectedText( ), select( ), isEditable( ), and setEditable( ) methods described in the preceding section.

TextArea adds the following editing methods:

 

void append(String str)

 

void insert(String str, int index)

 

void replaceRange(String str, int startIndex, int endIndex)

 

The append( ) method appends the string specified by str to the end of the current text. insert( ) inserts the string passed in str at the specified index. To replace text, call replaceRange( ). It replaces the characters from startIndex to endIndex–1, with the replacement text passed in str.

 

Text areas are almost self-contained controls. Your program incurs virtually no management overhead. Normally, your program simply obtains the current text when it is needed. You can, however, listen for TextEvents, if you choose.

The following program creates a TextArea control:

 

// Demonstrate TextArea.

import java.awt.*; import java.applet.*; /*

 

<applet code="TextAreaDemo" width=300 height=250> </applet>

 

*/

 

public class TextAreaDemo extends Applet { public void init() {

 

String val =

 

"Java 8 is the latest version of the most\n" +

 

"widely-used computer language for Internet programming.\n" + "Building on a rich heritage, Java has advanced both\n" + "the art and science of computer language design.\n\n" +

 

"One of the reasons for Java's ongoing success is its\n" + "constant, steady rate of evolution. Java has never stood\n" + "still. Instead, Java has consistently adapted to the\n" + "rapidly changing landscape of the networked world.\n" + "Moreover, Java has often led the way, charting the\n" + "course for others to follow.";

 

TextArea text = new TextArea(val, 10, 30); add(text);

 

}

 

}

Here is sample output from the TextAreaDemo applet:



Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.