JDK-4102211 : KeyEvents/KeyListeners for TextFields are inconsistent on Win95/Solaris
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.1.5,1.1.6
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_95
  • CPU: x86
  • Submitted: 1998-01-07
  • Updated: 2008-04-16
Related Reports
Relates :  
Description
Name: bk70084			Date: 01/06/98


The handling of KeyEvents in TextFields is inconsistent between win_95 and 
Solaris.  Under Solaris, the TextField handles the KeyEvent, and then any KeyListeners
are given the KeyEvent.  Under Windows 95, however, the KeyListeners are given the
KeyEvent BEFORE the TextField handles it.  The result of this is, when in the 
KeyListener, it is unknown whether the text returned by TextField.getText() has been
updated with the new keypress.

1. Run the following program
2. Type a few characters in the top TextField
3. Press the "Check" Button
4. Notice that the characters reported in the bottom TextField and the characters
    reported by the "Check" Button differ by one character under Windows 95, but
    they are identical under Solaris.


//--------------MyFrame.java--------------------

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

public class MyFrame extends Frame implements KeyListener {

  Button checkButton;
  TextArea inputarea, outputarea;

  public MyFrame() {
    setLayout(new BorderLayout());
    inputarea = new TextArea();
    inputarea.addKeyListener(this);
    add("North", inputarea);

    outputarea = new TextArea();
    outputarea.setEditable(false);
    add("South", outputarea);

    checkButton = new Button("Check");
    checkButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                String current_text = inputarea.getText();
                int current_length = current_text.length();
                System.out.println("The current text is \"" + current_text +
                    "\" and it is " + current_length + " characters long.");
            }
        });
    add("Center", checkButton);

    setSize(400, 400);
    show();
  }

  public void keyPressed(KeyEvent ke) {}

  public void keyReleased(KeyEvent ke) {}

  public void keyTyped(KeyEvent ke) {
    if (ke.getSource() == inputarea) {
      String current_text = inputarea.getText();
      int current_length = current_text.length();
      outputarea.append("The current text is \"" + current_text +
            "\" and it is " + current_length + " characters long." + "\n");
    }
  }

  public static void main(String args[])
  {
    MyFrame mf = new MyFrame();
  }
}

(Review ID: 22728)
======================================================================

Comments
EVALUATION Name: rrT76497 Date: 06/24/98 MohanRaj 25June98 Bug reproduced as reported ====================================================================== Name: rrT76497 Date: 06/24/98 . ====================================================================== 5/21/2000 kevin.ryan@eng -- as of 1.1.8_003 and 1.3.0-C, win32 and Solaris both present the same results in: a) in the lower textfield; and b) the related standard output. The # of characters reported in the lower textfield is always one less than the total # reported in the standard output. Since this particular inconsistency has been removed, this bug should be perhaps be closed, and remaining KeyEvent discrepancies handled via other open bugs such as # 4100317.
17-09-2004