TextArea

TextArea is the TextComponent for multiline input. Some constructors permit you to set the rows and columns of the TextArea on the screen. However, the LayoutManager may change your settings. As with TextField, the only way to limit the number of characters that a user can enter is to override the keyDown() method. The text in a TextArea appears left justified, and the justification is not customizable.

In Java 1.1, you can control the appearance of a TextArea scrollbar; earlier versions gave you no control over the scrollbars. When visible, the vertical scrollbar is on the right of the TextArea, and the horizontal scrollbar is on the bottom. You can remove either scrollbar with the help of several new TextArea constants; you can't move them to another side. When the horizontal scrollbar is not present, the text wraps automatically when the user reaches the right side of the TextArea. Prior to Java 1.1, there was no way to enable word wrap.

TextArea Variables

Constants

The constants for TextArea are new to Java 1.1; they allow you to control the visibility and word wrap policy of a TextArea scrollbar. There is no way to listen for the events when a user scrolls a TextArea.

TextArea Methods

Constructors

The following example uses the first four constructors. The results are shown in Figure 8.3. With the size-less constructors, notice that Windows creates a rather large TextArea. UNIX systems create a much smaller area. Depending upon the LayoutManager, the TextAreas could be resized automatically.

import java.awt.TextArea;
public class textas extends java.applet.Applet {
 public void init () {
 add (new TextArea ()); // A add (new TextArea (3, 10)); // B add (new TextArea ("Empty Area")); // C add (new TextArea ("Empty Area", 3, 10)); // D
}
} 

Figure 8.3: TextArea constructor

[Graphic: Figure 8-3]

Figure 8.4: TextArea policies

[Graphic: Figure 8-4]Setting text

The text-setting methods are usually called in response to an external event. When you handle the insertion position, you must translate it from the visual row and column to a one-dimensional position. It is easier to position the insertion point based upon the beginning, end, or current selection (getSelectionStart() and getSelectionEnd()).

Sizing Miscellaneous methods
java.awt.TextArea[text0,0,0,0x0,invalid,text="Empty Area", editable,selection=0-0, rows=3,columns=10, scrollbarVisibility=both] 

TextArea Events

With the 1.0 event model, the TextArea component can generate KEY_PRESS and KEY_ACTION (which calls keyDown()) along with KEY_RELEASE and KEY_ACTION_RELEASE (which called keyUp()). There is no ACTION_EVENT generated for TextArea.

NOTE:

The GOT_FOCUS and LOST_FOCUS events can be generated by this component but not reliably across platforms. Currently, they are generated on most UNIX platforms but not on Microsoft Windows/95 under Java 1.0. These events are generated under Java 1.1.

Similarly, the mouse events are not generated with JDK 1.0.2. See Appendix C for more information about platform dependencies.

With the Java 1.1 event model, there are no listeners specific to TextArea. You can register key, mouse, and focus listeners through the Component methods of addKeyListener(), addMouseListener(), and addFocusListener(), respectively. To register listeners for text events, call TextComponent.addTextListener(). Action

The TextArea component has no way to trigger the action event, since carriage return is a valid character. You would need to put something like a Button on the screen to cause an action for a TextArea. The following Rot13 program demonstrates this technique. The user enters text in the TextArea and selects the Rotate Me button to rotate the text. If the user selects Rotate Me again, it rotates again, back to the original position. Without the button, there would be no way to trigger the event. Figure 8.5 shows this example in action.

import java.awt.*;
public class Rot13 extends Frame {
 TextArea ta; Component rotate, done;
public Rot13 () {
 super ("Rot-13 Example"); add ("North", new Label ("Enter Text to Rotate:")); ta = (TextArea)(add ("Center", new TextArea (5, 40))); Panel p = new Panel (); rotate = p.add (new Button ("Rotate Me")); done = p.add (new Button ("Done")); add ("South", p);
}
public static void main (String args[]) {
 Rot13 rot = new Rot13(); rot.pack(); rot.show();
}
public boolean handleEvent (Event e) {
 if (e.id == Event.WINDOW_DESTROY) {
 hide(); dispose(); System.exit (0);
return true;
}
return super.handleEvent (e);
}
public boolean action (Event e, Object o) {
 if (e.target == rotate) {
 ta.setText (rot13Text (ta.getText()));
return true;
}
else if (e.target == done) {
 hide(); dispose(); System.exit (0);
}
return false;
}
String rot13Text (String s) {
 int len = s.length(); StringBuffer returnString = new StringBuffer (len); char c;
for (int i=0;i<len;i++) {
 c = s.charAt (i); if (((c >= 'A') && (c <= 'M')) || ((c >= 'a') && (c <= 'm'))) c += 13; 
else if (((c >= 'N') && (c <= 'Z')) || ((c >= 'n') && (c <= 'z'))) c -= 13;
returnString.append (c);
}
return returnString.toString();
}
} 

Figure 8.5: TextArea with activator button

[Graphic: Figure 8-5]Keyboard

Ordinarily, the TextArea component generates all the key events.

Mouse

Ordinarily, the TextArea component does not trigger any mouse events.

NOTE:

Mouse events are not generated for TextArea with JDK 1.0.2. See Appendix C for more information about platform dependencies.Focus

The TextArea component does not reliably generate focus events.

NOTE:

The GOT_FOCUS and LOST_FOCUS events can be generated by this component but not reliably across platforms. With the JDK, they are generated on most UNIX platforms but not on Microsoft Windows/95 under JDK 1.0. These events are generated with JDK 1.1. See Appendix C for more information about platform dependencies.

Listeners and 1.1 event handling

There are no listeners specific to the TextArea class. You can register Key, mouse, and focus listeners through the Component methods of addKeyListener(), addMouseListener(), and addFocusListener(), respectively. Also, you register listeners for text events by calling TextComponent.addTextListener().