I am trying to use KeyEvent to limit the characters in a jTextField that it can take as input on the KeyTyped event using the following code:
private void userIDFieldKeyTyped(java.awt.event.KeyEvent evt) {
char c = evt.getKeyChar();
if(c!=KeyEvent.VK_BACK_SPACE && c!=KeyEvent.VK_DELETE){
if (Character.isLetter(c) && Character.isDigit(c)){
} else {
evt.consume();
}
}
}
But the above code is not working as expected, it do not take any character as input, as it is clear from the code I am trying to input only AlphaNumeric characters, is there any way to remove this problem? Also I have tried using KeyEvent.VK_ALPHANUMERIC
but it didn't work either.
Edit: What I mean by limiting the characters is that the text field can take input only certain characters which I have allowed and not that to stop takinginput after a certain number of characters has been alread been entered.