Input Fields

Contents:
Text Component
TextField
TextArea
Extending TextField

There are two fundamental ways for users to provide input to a program: they can type on a keyboard, or they can select something (a button, a menu item, etc.) using a mouse. When you want a user to provide input to your program, you can display a list of choices to choose from or allow the user to interact with your program by typing with the keyboard. Presenting choices to the user is covered in Pick Me. As far as keyboard input goes, the java.awt package provides two options. The TextField class is a single line input field, while the TextArea class is a multiline one. Both TextField and TextArea are subclasses of the class TextComponent, which contains all the common functionality of the two. TextComponent is a subclass of Component, which is a subclass of Object. So you inherit all of these methods when you work with either TextField or TextArea.

Text Component

TextField and TextArea classes are fairly robust. However, in order to reduce duplication between the classes, they both inherit a number of methods from the TextComponent class. The constructor for TextComponent is package private, so you cannot create an instance of it yourself. Some of the activities shared by TextField and TextArea through the TextComponent methods include setting the text, getting the text, selecting the text, and making it read-only.

TextComponent Methods

Contents

Both TextField and TextArea contain a set of characters whose content determines the current value of the TextComponent. The following methods are usually called in response to an external event.

Text selection

Users can select text in TextComponents by pressing a mouse button at a starting point and dragging the cursor across the text. The selected text is displayed in reverse video. Only one block of text can be selected at any given time within a single TextComponent. Once selected, this block could be used to provide the user with some text-related operation such as cut and paste (on a PopupMenu).

Depending on the platform, you might or might not be able to get selected text when a TextComponent does not have the input focus. In general, the component with selected text must have input focus in order for you to retrieve any information about the selection. However, in some environments, the text remains selected when the component no longer has the input focus.

Carets

Introduced in Java 1.1 is the ability to set and get the current insertion position within the text object.

Read-only text

TextComponent is editable. If a user types while the component has input focus, its contents will change. A TextComponent can also be used in an output-only (read-only) mode.

The following listing is an applet that toggles the editable status for a TextArea and sets a label to show the current status. As you can see in Figure 8.1, platforms can change the display characteristics of the TextComponent to reflect whether the component is editable. (Windows darkens the background. Motif and Windows do nothing.)

import java.awt.*; import java.applet.*;
public class readonly extends Applet {
 TextArea area; Label label;
public void init () {
 setLayout (new BorderLayout (10, 10)); add ("South", new Button ("toggleState")); add ("Center", area = new TextArea ("Help Me", 5, 10)); add ("North", label = new Label ("Editable", Label.CENTER));
}
public boolean action (Event e, Object o) {
 if (e.target instanceof Button) {
 if ("toggleState".equals(o)) {
 area.setEditable (!area.isEditable ()); label.setText ((area.isEditable () ? "Editable" : "Read-only"));
return true;
}
} return false;
}
} 

Figure 8.1: Editable and read-only TextAreas

[Graphic: Figure 8-1]Miscellaneous methods

TextComponent Events

With the 1.1 event model, you can register listeners for text events. A text event occurs when the component's content changes, either because the user typed something or because the program called a method like setText(). Listeners are registered with the addTextListener() method. When the content changes, the TextListener.textValueChanges() method is called through the protected method processTextEvent(). There is no equivalent to TextEvent in Java 1.0; you would have to direct keyboard changes and all programmatic changes to a common method yourself.

In addition to TextEvent listeners, Key, mouse, and focus listeners are registered through the Component methods addKeyListener(), addMouseListener(), addMouseMotionListener(), and addFocusListener(), respectively. Listeners and 1.1 event handling

// Java 1.1 only import java.applet.*; import java.awt.*; import java.awt.event.*; class TextFieldSetter implements ActionListener {
 TextField tf; TextFieldSetter (TextField tf) {
 this.tf = tf;
}
public void actionPerformed(ActionEvent e) {
 if (e.getActionCommand().equals ("Set")) {
 tf.setText ("Hello");
}
}
}
public class text13 extends Applet implements TextListener {
 TextField tf; int i=0;
public void init () {
 Button b; tf = new TextField ("Help Text", 20); add (tf); tf.addTextListener (this); add (b = new Button ("Set")); b.addActionListener (new TextFieldSetter (tf));
}
public void textValueChanged(TextEvent e) {
 System.out.println (++i + ": " + e);
}
}