United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4140413 : JTextField keyTyped is called before character is added to the TextField

Details
Type:
Bug
Submit Date:
1998-05-20
Status:
Closed
Updated Date:
1998-05-22
Project Name:
JDK
Resolved Date:
1998-05-22
Component:
client-libs
OS:
windows_nt
Sub-Component:
javax.swing
CPU:
x86
Priority:
P4
Resolution:
Not an Issue
Affected Versions:
1.2.0
Fixed Versions:

Related Reports

Sub Tasks

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
                                     
1998-05-22



Hardware and Software, Engineered to Work Together