I tried this, but the JTextField keeps showing the last key entered. What I'm I doing wrong?...
private void codigoKeyTyped(java.awt.event.KeyEvent evt) {
//codigo is the JTextField
String s = String.valueOf(evt.getKeyChar());
try{
Integer.parseInt(s);
}catch(Exception e){
codigo.setText("");
}
}
Lastly, I want it to delete the last character and leave the integer value in the text field. But I guess that would be simple after solving that. Thanks!
//ANSWER!
Thanks everybody to answer my question. Finally I made this
private void codigoKeyTyped(java.awt.event.KeyEvent evt) {
char c = evt.getKeyChar();
if (! Character.isDigit(c)) {
evt.consume();
}
}