Java's Basic User Interface Components - TextArea
(Page 10 of 10 )
The TextArea is an editable text field that can handle more than one line of input. Text areas have horizontal and vertical scrollbars to scroll through the text. Adding a text area to a container is similar to adding a text field. To create a text area use one of the following constructors:
- TextArea() creates an empty text area with unspecified width and height.
- TextArea(int, int) creates an empty text area with indicated number of lines and specified width in characters.
- TextArea(String) creates a text area initialized with the given string.
- TextField(String, int, int) creates a text area containing the indicated text and specified number of lines and width in the characters.
The Applet in
Example 8 displays a text area that is filled with a string, when the programs begins running using appletviewer.
/*
<Applet code= “TextAreaTest.class”
Width = 250
Height = 450>
</Applet>
*/
Import java.awt.*;
Public class TextfieldTest extends java.applet.Applet
{
String letter = “Dear Readers: \n” +
“We are learning about Java AWT. \n” +
“Some of you might find it all exciting, while a few are still not very sure \n” +
“For those cat on the wall folks \n” +
“My advice is read it again and practice some codes. \n \n” +
“So brewing a cup of Java? Isn’t so hard as it looked…is it! ” ;
TextArea ltArea;
Public void init(){
ltArea = new TextArea(letter, 10, 50)
add(ltArea);
}
}
The TextArea, like TextField, can use methods like setText(), getText(), setEditable(), and isEditable(). In addition, there are two more methods like these. The first is the insertText(String, int) method, used to insert indicated strings at the character index specified. The second is replaceText(String, int, int), used to replace text between given integer position with the indicated string.
It's this sort of real work in Java applets and applications for which Java's Abstract Windowing Toolkit, or AWT, was designed. You've actually been using the AWT all along, as you might have guessed from the classes you've been importing. The basic idea behind the AWT is that a graphical Java program is a set of nested components, starting from the outermost window all the way down to the smallest UI component. Components can include things you can actually see on the screen, such as windows, menu bars, buttons, and text fields, and they can also include containers, which in turn can contain other components. Hope you have got a clear picture of Java AWT and all the basic UI components, in the next part of the tutorial we will deal with more advance user interface components.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |