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

I have used JFormattedTextField

with the NumberFormat

following:

-Creat a JFormattedTextField

refernce

JFormattedTextField integerField;

      

-Create a NumberFormat

refernce

NumberFormat integerFieldFormatter;

      

-In the constructor:

integerFieldFormatter = NumberFormat.getIntegerInstance();
integerFieldFormatter.setMaximumFractionDigits(0);

integerField = new JFormattedTextField(integerFieldFormatter );
integerField.setColumns(5);

      

..........

I wanted to use it with integers only, but when I type numbers like 1500, it converts after losing focus to 1500 and the exception is the first line:

Exception on stream "AWT-EventQueue-0" java.lang.NumberFormatException: for input line: "1,500"

When I use JTextField

instead of JFormattedTextField

All integers accepted ok, But the reason I want to use JFormattedTextField

is to take advantage of the input constraint.

+3


source to share


3 answers


I found a solution to my problem; Here :

The exact problem is that when I use JFormattedTextField

with NumberFormat

, JFormattedTextField adds a comma ',' in front of any 3 digits, for example

1000 is displayed as 1000

10000 is displayed as 10000

1,000,000 made as 1,000,000

When I read the integer value from JFormattedTextField enter this line of code

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

      

The comma is read as part of the string; 1000 reads 1000 and this string value cannot be converted to an integer value and therefore an 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 character ','

in the returned string and will convert normally to int

as expected.

Hello

+1


source


I realize this is an old question, but I just stumbled upon it for the same problem. Since the other answers seemed like workarounds, I went into the NumberFormat methods in more detail.

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

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

      



This way, the group separators will not appear in the text box output.

Of course, you won't be able to use them for input either, but that wasn't asked by the question, right?

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

+7


source


You can do this in (at least) two 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 DocumentFilter check this link: How to allow only numbers to be entered in jTextField?

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

You don't know in what order the 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 be used before it reaches you)

EDIT # 2: ANOTHER FAST WAY

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

      

And the trick has to be done. with this you can insert up to 5 digits in the tour text field, more "#" in the string parameter for formatter = more digits can be entered by the user

0


source







All Articles