add listener for javafx textField upto 2 decimal place

后端 未结 2 925
我寻月下人不归
我寻月下人不归 2021-01-14 11:09

I want to set javaFX text field upto two decimal places . I found the answer but it is for numeric value . e-g

 // force the field to be numeric only
textFie         


        
2条回答
  •  太阳男子
    2021-01-14 11:18

    I couldn't resist. This is aboves answer in two lines (the ones that do all the work)

    private static TextFormatter new3DecimalFormatter(){
            Pattern decimalPattern = Pattern.compile("-?\\d*(\\.\\d{0,3})?");
            return new TextFormatter<>(c -> (decimalPattern.matcher(c.getControlNewText()).matches()) ? c : null );
    }
    

提交回复
热议问题