Search code examples
javaswingnumbersjtextfield

Can't Set textfield to Digits & commas/dots only


I'm trying to set up a program to help myself at work but I still can't cause, I can't add digit numbers to be allowed in this code:

char inte=evt.getKeyChar();
    if (!(Character.isDigit(inte)|| inte==KeyEvent.VK_BACK_SPACE) || inte==KeyEvent.VK_DELETE){
    getToolkit().beep();
        evt.consume();

So, im trying to get to a point to be able to add commas because i need to use floats.

Any ideas?


Solution

  • This piece of code should help you:

    char c = evt.getKeyChar();
    if (!(Character.isDigit(c) || c == KeyEvent.VK_COMMA || c == KeyEvent.VK_DELETE || c == KeyEvent.VK_BACK_SPACE)) {
        getToolkit().beep();
        evt.consume();
    }