Is there any way to accept only numeric values in a JTextField?

前端 未结 19 1915
陌清茗
陌清茗 2020-11-22 03:10

Is there any way to accept only numeric values in a JTextField? Is there any special method for this?

19条回答
  •  误落风尘
    2020-11-22 03:53

    You would like to take a look at JFormattedTextField

    Formatted text fields provide a way for developers to specify the valid set of characters that can be typed in a text field

    This is a subclass of JTextField, so you can use it like this:

    JTextField textField = new JFormattedTextField(NumberFormat.getInstance());
    

提交回复
热议问题