To simplify user interaction and make data entry easier, you can use java controls. Controls are components, such as buttons, labels and text boxes, that can be added to containers like frames, panels and applets. The Java.awt package provides an integrated set of classes to manage user interface components.
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.
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.