BUG: AWT TextField Component Does Not Behave as Expected


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


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.


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

  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.

ID članka: 223429 - posljednja izmjena: 14. velj 2017. - verzija: 1

Microsoft Virtual Machine for Java, Microsoft Visual J++ 6.0 Standard Edition

Povratne informacije