home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


When the program runs, you’ll see a single text field that’s initialized with the value of “Hello TextField!”. If you click inside the text field with the mouse, you’ll see a message that says focus was gained. If you click outside the text field with the mouse, you’ll see a message that says focus was lost. If you edit the text field, you’ll see each figure you type appear in the display string that’s telling you which actions are being handled. If you press the Enter key, you’ll see that an action event was triggered. Figure 7.6 shows the program running within Internet Explorer.


Figure 7.6  This program handles events for a TextField control.

This time, I’ll go in sequential order through the source code instead of skipping to a point halfway down the example. Looking at line 9, you can see that this is where the extended TextField class was created. Notice that the constructor takes a single string argument. This is the only constructor we’ve implemented. At line 13, the TextField object is added to the applet. Line 21 is where the extended class begins. Notice that at line 24, the one and only constructor begins. Pay attention to line 27. It’s at this line where we enable the events. Notice that we’re enabling three separate event types: action events, focus events, and key events. At line 31, the method that handles key events can be seen. It’s the processKeyEvent() method. At line 41, the method that handles action events, the processActionEvent() method, can be seen. Lastly, at line 48, the method that handles focus events can be seen—the processFocusEvent() method.

The TextArea Control

The TextArea control can contain multiple lines of text. Like the TextField control, TextArea controls can be created empty or with an initial string. TextArea controls can be defined with an initial number of rows and columns. The TextArea control inherits most of its functionality from the Text control.

Constructors

There are five constructors for creating TextArea controls. The first constructor takes no arguments and creates an empty TextArea control. The following example shows how to do this:

TextArea text = new TextArea();

The second constructor lets you create a text area and specify the number of rows and columns. It takes two integer arguments, the first one being the number of rows you want, and the second one being the number of columns you want. The following example lets you create a text field of 25 rows and 45 columns:

TextArea text = new TextArea( 25, 45 );

The third constructor takes a single argument. That argument is a string. When created, this string will be displayed in the text area. The following example lets you create a text area with a string in it:

TextArea text = new TextArea( "Hello there TextArea!" );

The fourth constructor takes three arguments. The first argument is the string that will be initially placed in the text area. The second argument is the number of rows you want, and the third argument is the number of columns you want the text area to have. The next example shows how to create a TextArea object with a string, 25 rows and 45 columns:

TextArea text = new TextArea( "Hello there TextArea!", 25, 45 );

The fifth and final constructor is almost identical to the preceding one we discussed, except that it adds a fourth argument that determines how the text area’s scrollbars are managed. Table 7.1 shows the available values. Following is an example of the final constructor:

TextArea text = new TextArea( "Hello there TextArea!", 25, 45,
TextArea.SCROLLBARS_NONE );
Table 7.1 Available Scrollbar Values for Text Areas

Value Description

SCROLLBARS_BOTH Have both vertical and horizontal scrollbars.
SCROLLBARS_VERTICAL_ONLY Have only vertical scrollbars.
SCROLLBARS_HORIZONTAL_ONLY Have only horizontal scrollbars.
SCROLLBARS_NONE Allow no scrollbars.

Events

TextAreas do not generate action events. They do, however, generate keyboard, focus, and text events. These events are handled exactly in the same way as TextField events.

The Label Control

The most basic user interface control is the Label. The Label is a text string that is usually used to indicate the name of other user-interface components. Label components can be assigned an arbitrary text string and alignment attribute. After a label has been created, the text string and alignment can be queried and modified.

Constructors

Three constructors are available when you create Label controls. The first constructor takes no arguments. The next example shows how to create a label using this constructor:

Label label = new Label();

Normally, though, when you create a label, you tell it what text to use. The second label constructor takes a single argument, which is a string. This is the string that will be displayed in the label. The next example shows how to create a label with the text “Hello Label”:

Label label = new Label( "Hello Label" );

The last way to create a Label control is with a constructor that takes two arguments. The first argument is a string that determines the text that will be in the label, and the second argument is the alignment. Three alignment types are available: label.LEFT, label.CENTER, and label.RIGHT. The following example shows how to create a label with the text “Hello Label” and an alignment of LEFT:

Label label = new Label( "Hello Label", Label.LEFT );


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.