I am trying to create a jTextField that only accepts 0-9 and one “.” ( that is currency format).
- But processing key event does not display anything on the textbox
- But keycode is printed on console properly.
What am I doing wrong
I am using
- NetBeans 22 on Windows 11
- openjdk version “20” 2023-03-21
here is the code
txtMrp = new javax.swing.JTextField()
{
@Override
public void processKeyEvent(KeyEvent ev) {
int c = ev.getKeyCode();
System.out.println("Keycode: " + c);
if (
(c>=KeyEvent.VK_0 && c<=KeyEvent.VK_9) ||
(c>=KeyEvent.VK_NUMPAD0 && c<=KeyEvent.VK_NUMPAD9) ||
c == KeyEvent.VK_BACK_SPACE ||
c == KeyEvent.VK_DELETE ||
c == KeyEvent.VK_LEFT ||
c == KeyEvent.VK_RIGHT ||
(c==KeyEvent.VK_PERIOD && !this.getText().contains("."))
) {
super.processKeyEvent(ev);
}
ev.consume();
return;
}
}
;
here is the screenshots
2