How to force a double input in a TextField in JavaFX?

前端 未结 3 868
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-06 19:24

How to make sure an user inputs only double values in a given TextField?

I found a solution for integers bu I can\'t manage to use it for doubles.

3条回答
  •  失恋的感觉
    2021-01-06 19:56

    You could make use of Double.parseDouble Double Javadoc. This will do the parsing for you, rather than writing your own double validation. You can then check for a number format exception or null pointer exception.

        try
        {
            Double.parseDouble(newValue);
            // Valid double
        }
        catch (NullPointerException | NumberFormatException ex)
        {
            // Not valid double
        }
    

提交回复
热议问题