BUG: AWT TextField Component Does Not Behave as Expected (223429)



The information in this article applies to:

  • Microsoft virtual machine
  • Microsoft Visual J++ 6.0

This article was previously published under Q223429

SYMPTOMS

When you use a combination the select, focusLost, focusGained, and setText methods of the AWT TextField component, the component does not behave as expected.

RESOLUTION

To greatly reduce the occurence of this bug, make two calls to setText each time you need to set the text for the TextField. For more information, see the commented code in the "More Information" section.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Compile and run the following code:
    import java.awt.*;
    import java.awt.event.*;
    
    public class Class1 {
    
       public static void main (String[] args) {
          Frame frame = new Frame();
          frame.setSize( 400, 50 );
    
          TextField textfield = new TextField( "Test", 20 );
          Button button = new Button("Have a Nice Day");
    
          frame.setLayout(new BorderLayout());
    
          FocusListener focusListener = new FocusAdapter() {
             public void focusGained( FocusEvent e ) {
                ((TextField)e.getComponent()).selectAll();
             }
    
             public void focusLost( FocusEvent e ) {
                ((TextField)e.getComponent()).select(0,0);
             }
          };
    
          WindowListener windowListener = new WindowAdapter() {
             public void windowClosing( WindowEvent e ) {
                ((Frame)e.getSource()).dispose();
                System.exit( 0 );
             }
          };
    
          ActionListener actionListener = new ActionListener() {
             public void actionPerformed( ActionEvent e ) {
                String s = ((TextField)e.getSource()).getText();
                ((TextField)e.getSource()).setText( s  + "." );
    
                // Workaround
                // Uncomment the following line to fix this problem
                //((TextField)e.getSource()).setText( s  + "." );
             }
          };
    
    
          frame.addWindowListener(windowListener);
    
          textfield.addFocusListener(focusListener);
          textfield.addActionListener(actionListener);
    
          frame.add("West",textfield);
          frame.add("Center",button);
          frame.show();
       }
    
    }
    					
  2. Press ENTER. This appends "." to the TextField's text through a call to setText.
  3. Press the TAB key. This gives the button the focus.
  4. Press the TAB key again. This gives the TextField the focus, though the text is not highlighted as expected.
  5. Press the TAB key again. This gives the button the focus, and the text is now highlighted.
  6. When you press the TAB key repeatedly, the text remains highlighted.

Modification Type:MajorLast Reviewed:10/20/2003
Keywords:kbAwtPkg kbBug kbpending KB223429