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

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

I used JFormattedTextField withNumberFormat in this way:

-Creat a JFormattedTextField refernce

JFormattedTextF         


        
4条回答
  •  清歌不尽
    2021-01-14 07:46

    I realize this is an old question but I just stumbled upon it through the same issue. As the other answers seemed like workarounds to me, I took a closer look at the NumberFormat methods.

    I found that the easiest approach would actually be to simply deactivate grouping on the NumberFormat instance:

    NumberFormat integerFieldFormatter = NumberFormat.getIntegerInstance();
    integerFieldFormatter.setGroupingUsed(false);
    

    That way no group delimiters will appear in the textfield output.

    Of course you will also not be able to use them for your input, but that was not intended by the question, right?

    Also for an integer instance of NumberFormat you don't need to explicitly setMaximumFractionDigits(0), as that is part of what getIntegerInstance() does for you.

提交回复
热议问题