-
Bug
-
Resolution: Fixed
-
P2
-
1.1.6
-
1.1.8
-
x86
-
windows_nt
-
Verified
Name: diC59631 Date: 11/19/98
In a java.awt.TextField that should consume all keys except numerical keys and periods, the periods are erroneously consumed.
This is due to the fact that the keyEvent.getKeyCode returns another value than is defined in the constant KeyEvent.VK_PERIOD.
I noticed this bug only under Windows NT with both the jvm 1.1.6 and jvm 1.1.7. Under OS/2 this bug is not seen under jvm 1.1.6
To reproduce the bug, run the accompanying program.
-----------------------------------------------------
import java.awt.*;
import java.awt.event.*;
/**
* A Textfield that only accepts numbers and periods.
* It shows a bug on Windows NT JDK 1.1.6 and JDK 1.1.7 where periods are
* erroneously consumed. The println-statement in the method keyPressed
* show why. The KeyEvent constant VK_PERIOD has another value than the
* keycode that is actually passed to the event handler
*/
public class BuggedPeriodField extends TextField implements KeyListener, WindowListener {
private boolean mustConsume = false;
public BuggedPeriodField() {
addKeyListener(this);
}
public void keyPressed(KeyEvent e) {
mustConsume = true;
int keyCode = e.getKeyCode();
System.out.println("keyCode = " + keyCode + ", VK_PERIOD = " + KeyEvent.VK_PERIOD);
if (((keyCode >= KeyEvent.VK_0) && (keyCode <= KeyEvent.VK_9)) ||
(keyCode == KeyEvent.VK_PERIOD))
mustConsume = false;
if (mustConsume) e.consume();
}
public void keyReleased(KeyEvent e) {
if (mustConsume) e.consume();
}
public void keyTyped(KeyEvent e) {
if (mustConsume) e.consume();
}
public static void main(String args[]) {
Frame frame = new Frame("VK_PERIOD Bug");
frame.setLayout(new BorderLayout());
frame.add(new Label("Enter numbers and/or periods:"), BorderLayout.NORTH);
BuggedPeriodField buggedField = new BuggedPeriodField();
frame.addWindowListener(buggedField);
frame.add(buggedField, BorderLayout.SOUTH);
frame.setSize(300, 100);
frame.setVisible(true);
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {
System.exit(0);
}
public void windowClosing(WindowEvent e) {
e.getWindow().dispose();
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}
(Review ID: 42997)
======================================================================