TextField.setText fails to fire textValueChanged

(Sun bug # 5106832)

There are circumstances in which TextField.setText fails to fire textValueChanged, detailed here.

In the applet running running above (source code below), if textField.setText is called before the panel is added to the display the text is set in textField but textValueChanged is not fired (check in Java Console).  In contrast, if the setText method is moved after the add(panel) method the textValueChanged method is fired.

This is a problem because TextField.setText is supposed to fire textValueChanged in all situations.  Here is why this is a real life problem:

In more complicated programs it is not trivial to keep track of whether the container for the TextField was added.  This can cause real problems if the programmer needs to use flags to keep track of whether textValueChanged was fired from setText or from input from a pen (see Tablet input indistinguishable from setText changes for more details).  Uncertainty about whether textValueChanged was called leads to uncertainty as to whether the flag was reset and can lead to bugs that are difficult to identify.

Temporary workaround: call setText only after the container for its component has been added and tell other programmers to do the same until the bug is fixed in all JVMs in the wild.

JVMs affected:

JVMs not affected:

If you have any insights, workarounds or comments about this test page please contact Mickey Segal.  A listing of  many Java resources is at this link.

Source code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class text_events extends Applet implements TextListener {

public void init()
    setBackground(new Color(225,225, 255));
    Panel panel = new Panel();
    TextField textField = new TextField(20);
    textField.setText("Initial text");

public void textValueChanged(TextEvent te)
    System.out.println("textValueChanged fired");
} // END OF Class text_events