JDK-4140413 : JTextField keyTyped is called before character is added to the TextField
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_nt
  • CPU: x86
  • Submitted: 1998-05-20
  • Updated: 1998-05-22
  • Resolved: 1998-05-22
Description

Name: rk38400			Date: 05/20/98


Registering a keyListener for the JTextField
component, and defining the keyTyped method
for that interface one can quickly see that
the keyTyped event is fired BEFORE the char
is added to the JTextField. It should be
called after the character has been added.
This is still the case under Swing-1.0.2.
The following simple example shows this.

/* ---------------------- */

import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;

public class test extends JFrame {
  JTextField tf;

  public test()
  {
    getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
    tf = new JTextField(30);
    getContentPane().add(tf);
    tf.addKeyListener(new keyboardHandler());
    setSize(200,100);
  }

  class keyboardHandler extends KeyAdapter
  {
    public void keyTyped(KeyEvent event) {
      System.out.println(tf.getText().length());
    }
  }

  public static void main(String [] args)
  {
    (new test()).show();
  }
}
(Review ID: 30689)
======================================================================

Comments
EVALUATION The listeners are notified of the key events prior to processing them to allow the listeners to "steal" the events by consuming them. This gives compatibility with the older awt notion of consuming events. The "typed" event does not mean text was entered into the component. This is NOT a bug, it is intended behavior. timothy.prinzing@eng 1998-05-22
22-05-1998