How to implement a NumberField in javaFX 2.0?

后端 未结 8 1555
慢半拍i
慢半拍i 2020-12-14 17:54

I need to insert a number field into my UI. So I need to check the key events on a text field in order to check whether the input character is a number. I created a class by

8条回答
  •  执念已碎
    2020-12-14 18:28

    Applying the following way it is possible to make TextField into NumberField but this way it will not filter the keyboard input instantly... but if you focus out the field then you will see the result. You may use this for basic numeric input field validation. I am giving here some example... It may help someone :)

    // It will show the number with comma ie. 64,568,455
    TextField numberField = new TextField();
    numberField.setTextFormatter(new TextFormatter<>(new NumberStringConverter()));
    
    // It will show the integer number ie. 64568455
    TextField integerField = new TextField();
    integerField.setTextFormatter(new TextFormatter<>(new IntegerStringConverter()));
    
    // It will ensure the float format ie. 2635.0
    TextField floatField = new TextField();
    floatField.setTextFormatter(new TextFormatter<>(new FloatStringConverter()));
    

提交回复
热议问题