Restricting a TextField input to hexadecimal values in Java FX

后端 未结 3 1806
Happy的楠姐
Happy的楠姐 2021-01-21 20:37

How can I restrict the input from the user to only hexadecimal values? With decimal notation the range is from 0 to 16383, but I would like to let the user type an hexadecimal n

3条回答
  •  旧巷少年郎
    2021-01-21 21:23


    best way to do it is with regular expressions:

    yourTextField.textProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue observable, String oldValue,
                                String newValue) {
                if (!newValue.matches("^[0-9A-F]+$")) {
                    yourTextField.setText(newValue.replaceAll("[^\\d]", ""));
                }
            }
        });
    

    Is this what your are looking for?

提交回复
热议问题