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

与世无争的帅哥 提交于 2019-12-01 08:39:24

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.

Saleh Feek

I discovered the solution to my problem; Here it is:

The exact problem is that when I use JFormattedTextField with NumberFormat, the JFormattedTextField adds comma ',' before any next 3 digits for example

1000 rendered as 1,000

10000 rendered as 10,000

1000000 rendered as 1,000,000

When I read an integer value from JFormattedTextField usign this line of code

  int intValue = Integer.parseInt(integerField.getText());

The comma is read as part of the string; 1000 read as 1,000 and this string value cannot be converted to integer value, and so exception is thrown.

Honestly the solution is in this Answer but I will repeat it here

use str.replaceAll(",","")

 int intValue = Integer.parseInt(integerField.getText().replaceAll(",", ""));

This will replace any comma charachter ',' in the returned string and will be converted normally to int as expected.

Regards

BackSlash

You can do it in (at least) 2 ways:

  • Using a keyListener
  • Using DocumentFilter

if you want to use KeyListener:

KeyListener listener = new KeyAdapter(){
    public void keyTyped(KeyEvent e){
        if(e.getKeyCode()<KeyEvent.VK_0||e.getKeyCode()>KeyEvent.VK_9{//input<'0' or input>'9'?
            e.consume();//delete the typed char
        }
    }
}

yourTextField.addKeyListener(listener);

to use the DocumentFilter check this link: How to allow introducing only digits in jTextField?

EDIT: i forgot to say this. As MadProgrammer said in the first comment to this answer, KeyListener is not the proper way to do it, because

You do not know in what order KeyListeners will be notified of the event and the key may have already gone to the text component before it reaches you (or it could have been consumed before it reaches you)

EDIT #2: ANOTHER QUICK WAY

MaskFormatter formatter = new MaskFormatter("#####");
JFormattedTextField field = new JFormattedTextField(formatter);

And the trick should be done. with this you can insert up to 5 digits in tour textfield, more '#' in the string parameter for the formatter = more digits can be typed by the user

Vaibhav Nandkule

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");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!