How to make JFormattedTextField accept integer without decimal point (comma)?

后端 未结 4 1565
别跟我提以往
别跟我提以往 2021-01-14 06:58

I used JFormattedTextField withNumberFormat in this way:

-Creat a JFormattedTextField refernce

JFormattedTextF         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-14 07:33

    Try this, This is a complete solution for creating and validating number JTextField

    NumberFormat format = NumberFormat.getInstance();
    format.setGroupingUsed(false);//Remove comma from number greater than 4 digit
    NumberFormatter sleepFormatter = new NumberFormatter(format);
    sleepFormatter.setValueClass(Integer.class);
    sleepFormatter.setMinimum(0);
    sleepFormatter.setMaximum(3600);
    sleepFormatter.setAllowsInvalid(false);
    
    sleepFormatter.setCommitsOnValidEdit(true);// committ value on each keystroke instead of focus lost
    
    JFormattedTextField textFieldSleep = new JFormattedTextField(sleepFormatter);
    textFieldSleep.setText("0");
    

提交回复
热议问题