Comma-free NumberFormat text box

I have a JFormattedTextField that I want to accept digits in a 5 digit range. The following code works correctly:

myNumberBox = new JFormattedTextField(NumberFormat.getIntegerInstance()); 

However, when I enter “12345” into the field and switch focus, the comma is inserted because of my locale, creating the text “12,345”. How can I prevent commas from being added to my contribution? Even better is it possible to delete them even if the user inserts commas?

+5
source share
1 answer

You must disable grouping in the NumberFormat object as follows:

 NumberFormat format = NumberFormat.getIntegerInstance(); format.setGroupingUsed(false); myNumberBox = new JFormattedTextField(format); 

See: NumberFormat.setGroupingUsed

+11
source

All Articles