TextField

TextField is the TextComponent for single-line input. Some constructors permit you to set the width of the TextField on the screen, but the current LayoutManager may change it. The text in the TextField is left justified, and the justification is not customizable. To change the font and size of text within the TextField, call setFont() as shown in Fonts and Colors.

The width of the field does not limit the number of characters that the user can type into the field. It merely suggests how wide the field should be. To limit the number of characters, it is necessary to override the keyDown() method for the Component. Extending TextField contains an example showing how to do this.

TextField Methods

Constructors

The following example uses all four constructors; the results are shown in Figure 8.2. With the third constructor, you see that the TextField is not quite wide enough for our text. The system uses an average width per character to try to determine how wide the field should be. If you want to be on the safe side, specify the field's length explicitly, and add a few extra characters to ensure that there is enough room on the screen for the entire text.

import java.awt.TextField;
public class texts extends java.applet.Applet {
 public void init () {
 add (new TextField ()); // A add (new TextField (15)); // B add (new TextField ("Empty String")); // C add (new TextField ("Empty String", 20)); // D
}
} 

Figure 8.2: Using the TextField constructors

[Graphic: Figure 8-2]Sizing

Echoing character

It is possible to change the character echoed back to the user when he or she types. This is extremely useful for implementing password entry fields.

Miscellaneous methods
java.awt.TextField[0,0,0x0,invalid,text="Empty String",editable,selection=0-0] 

TextField Events

With the 1.0 event model, TextField components can generate KEY_PRESS and KEY_ACTION (which calls keyDown()), KEY_RELEASE and KEY_ACTION_RELEASE (which calls keyUp()), and ACTION_EVENT (which calls action()).

With the 1.1 event model, you register an ActionListener with the method addActionListener(). Then when the user presses Return within the TextField the ActionListener.actionPerformed() method is called through the protected TextField.processActionEvent() method. Key, mouse, and focus listeners are registered through the three Component methods of addKeyListener(), addMouseListener(), and addFocusListener(), respectively. Action

Keyboard Mouse

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

NOTE:

Mouse events are not generated for TextField with JDK 1.0.2. Your run-time environment may behave differently. See Appendix C for more information about platform dependencies.Focus

The TextField component does not reliably generate focus events.

NOTE:

The GOT_FOCUS and LOST_FOCUS events can be generated by TextFields, but these events are not reliable across platforms. With Java 1.0, they are generated on most UNIX platforms but not on Windows/95 platforms. They are generated on all platforms under Java 1.1. See Appendix C for more information about platform dependencies.

Listeners and 1.1 event handling

With the 1.1 event model, you register event listeners that are told when an event occurs. You can register text event listeners by calling the method TextComponent.addTextListener().

// Java 1.1 only import java.applet.*; import java.awt.*; import java.awt.event.*; class MyAL implements ActionListener {
 public void actionPerformed(ActionEvent e) {
 System.out.println ("The current text is: " + e.getActionCommand()); if (e.getSource() instanceof TextField) {
 TextField tf = (TextField)e.getSource(); StringBuffer sb = new StringBuffer (e.getActionCommand()); tf.setText (sb.reverse().toString());
}
}
}
public class text11 extends Applet {
 public void init () {
 TextField tf = new TextField ("Help Text", 20); add (tf); tf.addActionListener (new MyAL());
}
} 

The following applet is equivalent to the previous example, except that it overrides processActionEvent() to receive events, eliminating the need for an ActionListener. The constructor calls enableEvents() to make sure that events are delivered, even if no listeners are registered.

// Java 1.1 only import java.applet.*; import java.awt.*; import java.awt.event.*; class MyTextField extends TextField {
 public MyTextField (String s, int len) {
 super (s, len); enableEvents (AWTEvent.ACTION_EVENT_MASK);
}
protected void processActionEvent(ActionEvent e) {
 System.out.println ("The current text is: " + e.getActionCommand()); TextField tf = (TextField)e.getSource(); StringBuffer sb = new StringBuffer (e.getActionCommand()); tf.setText (sb.reverse().toString()); super.processActionEvent(e)
}
}
public class text12 extends Applet {
 public void init () {
 TextField tf = new MyTextField ("Help Text", 20); add (tf);
}
}