United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-5046125 REGRESSION: Textfield.keyPressed().KeyEvent.consume() fails to consume key event
JDK-5046125 : REGRESSION: Textfield.keyPressed().KeyEvent.consume() fails to consume key event

Details
Type:
Enhancement
Submit Date:
2004-05-12
Status:
Open
Updated Date:
2006-07-18
Project Name:
JDK
Resolved Date:
Component:
client-libs
OS:
linux
Sub-Component:
java.awt
CPU:
x86
Priority:
P3
Resolution:
Unresolved
Affected Versions:
5.0
Targeted Versions:

Related Reports

Sub Tasks

Description
Name: rmT116609			Date: 05/12/2004


FULL PRODUCT VERSION :
java version "1.5.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b32c)
Java HotSpot(TM) Client VM (build 1.5.0-beta-b32c, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Linux MyLaptop 2.4.18-14 #1 Wed Sep 4 13:35:50 EDT 2002 i686 i686 i386 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
Every letter, number entered is duplicated when painted in the textfield.
even though i have an evt.consume() the keyevent appears to be passed along to other keyevent listeners. I only listen to keyPressed events.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
run example.
type a letter, or so in the textfield.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Only the String in the setText() should be on the screen.
ACTUAL -
the character typed in is duplicated

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
/*
 * TEXTFIELDbug.java
 *
 * Created on May 11, 2004, 6:59 PM
 */

/**
 *
 * @author  gat
 */
public class TEXTFIELDbug extends java.awt.Frame {
    
    /** Creates new form TEXTFIELDbug */
    public TEXTFIELDbug() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        tf2 = new java.awt.TextField();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        tf2.setColumns(35);
        tf2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tf2KeyPressed(evt);
            }
        });

        add(tf2, java.awt.BorderLayout.CENTER);

        pack();
    }//GEN-END:initComponents

    private void tf2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf2KeyPressed
        // Add your handling code here:
        evt.consume();
        char c = evt.getKeyChar();
        String s = tf2.getText();
        s = s + c;
        tf2.setText( s );
        //int i = tf2.getCaretPosition();
        //tf2.setCaretPosition( i++ );
        System.out.println( "TEXTFIELDbug.tf2KeyPressed(); setText()=\""+s+"\", getText()=\""+tf2.getText()+"\"");
    }//GEN-LAST:event_tf2KeyPressed
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        System.exit(0);
    }//GEN-LAST:event_exitForm
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new TEXTFIELDbug().show();
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.TextField tf2;
    // End of variables declaration//GEN-END:variables
    
}

---------- END SOURCE ----------

Release Regression From : 1.4.2
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.

(Incident Review ID: 265185) 
======================================================================

                                    

Comments
EVALUATION

Name: osR10079			Date: 05/13/2004

It is not documented which KeyEvent user should consume to leave text component unchanged.
In this particular case the cause of the problem is that we use Swing text components
in XAWT and these components are updated by KEY_TYPED.
So as a workaround user should consume KEY_TYPED.  Also I would recommend
to consume all three key event (pressed, typed and released).

Although we have incompatibility and inconsistency here.  We should try to fix it
in next release.
###@###.### 2004-05-13
======================================================================
                                     
2004-05-13
EVALUATION

Changing type to RFE. It would be good to make keyTyped consume() results consistent across platforms and releases, and perhaps to provide some documentation on this.
                                     
2006-07-17



Hardware and Software, Engineered to Work Together