Control keyboard input into javafx TextField

后端 未结 6 1750
清酒与你
清酒与你 2020-12-31 17:44

I want to control the input into a Javafx TextField so that I can allow only Numeric input, and so that if the max characters are exceeded, then no change will be made to th

6条回答
  •  天命终不由人
    2020-12-31 17:58

    I simply set the 'On Key Typed' event to run this small procedure:

        @FXML public void processKeyEvent(KeyEvent ev) {
        String c = ev.getCharacter();
        if("1234567890".contains(c)) {}
        else {
            ev.consume();
        }
    }
    

    It works like a champ!

提交回复
热议问题