Microsoft KB Archive/223429

= BUG: AWT TextField Component Does Not Behave as Expected =

Article ID: 223429

Article Last Modified on 10/20/2003

-

APPLIES TO


 * Microsoft Java Virtual Machine
 * Microsoft Visual J++ 6.0 Standard Edition

-



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.



Steps to Reproduce Behavior
  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; }

}                    Press ENTER. This appends "." to the TextField's text through a call to setText. Press the TAB key. This gives the button the focus. Press the TAB key again. This gives the TextField the focus, though the text is not highlighted as expected. Press the TAB key again. This gives the button the focus, and the text is now highlighted. When you press the TAB key repeatedly, the text remains highlighted.</li></ol>

Keywords: kbbug kbawtpkg kbpending KB223429

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.