JDK-4059600 : KeyTyped KeyEvent and Special Characters (not able to filter them out)
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.1.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_95
  • CPU: x86
  • Submitted: 1997-06-18
  • Updated: 2000-06-03
  • Resolved: 2000-06-03
Related Reports
Duplicate :  
Relates :  
Description

Name: tb29552			Date: 06/17/97


Hello,

I intended to create a text field in which it is possible to
insert digits only. The source is appended below. The resulting
code screens some characters out (especially normal alphabetic
characters, a-z and A-Z for example), but it is still possible to
input some others (especially accented and special characters,
�������������� for example).

Is this a bug, or am I doing something wrong?

Thank you for your support.

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

class IntField extends TextField implements KeyListener {

    IntField() {
        addKeyListener(this);
    }

    public void keyPressed(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {
    }

    public void keyTyped(KeyEvent e) {
        char c = e.getKeyChar();
        if ((c != '\b') & !Character.isDigit(c))
            e.consume();
    }

    public static final void main(String[] args) {
        Frame f = new Frame("IntField Test");
        IntField i = new IntField();
        f.add(i);
        f.pack();
        f.show();
    }

}

company - BCEE , email - ###@###.###
======================================================================

Comments
EVALUATION 6/3/2000 kevin.ryan@eng -- see also 4028470, 4114565 (am duping this bug to #4114565).
11-06-2004

WORK AROUND Name: tb29552 Date: 06/17/97 Problem submitted since no simple work around could be found. ====================================================================== 6/3/2000 kevin.ryan@eng -- do the filtering in keyPressed() vs. keyTyped(). Unfortunately, you must also consume the related keyReleased() event. As noted in bug # 4114565, this is not a desirable approach.
11-06-2004

PUBLIC COMMENTS since bug # 4114565 addresses this issue, and has several Bug Parade votes, am duping this bug to # 4114565.
10-06-2004