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

前端 未结 19 1918
陌清茗
陌清茗 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 04:03

    Use formatter to format text field.

    NumberFormat format = NumberFormat.getInstance();
    format.setGroupingUsed(false);
    NumberFormatter formatter = new NumberFormatter(format);
    formatter.setValueClass(Integer.class);
    formatter.setMaximum(65535);
    formatter.setAllowsInvalid(false);
    formatter.setCommitsOnValidEdit(true);
    myTextField = new JFormattedTextField(formatter);
    

提交回复
热议问题