I used JFormattedTextField
withNumberFormat
in this way:
-Creat a JFormattedTextField
refernce
JFormattedTextF
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