Hi to All! I have a POS Software which I have developed in Java, NetBeans IDE. I am deploying this software on a variety of Windows Computers / Laptops for 5 years. I have used JFormattedTextFields for double data type in the whole Software. And I have set Format of this field to two decimal places. For example if an user types 12.325 , then this field returns 12.32 , Taking another example if an user types 12 , then this field returns 12.00 This is the perfect functionality that I want and my Software is working fine on all the clients Desktops/Laptops.

But a weird problem occurred yesterday, when I gave demo to a new Italy Based Client. For example If I type/enter 12.35 in the JFormatedTextField it automatically replaces decimals with comma i-e it returns 12,35 Taking another example if You will type 5 , it is returning 5,00 automatically. And this is bug, I can't figure out why is this happening. First time I have faced this error. It is the same Software, same code and same Java Version. Kindly guide me about the problem

Note1- This error is occurring in case of JFormatedTextField only.
Note2- The Clients computer has Windows 10 Pro (But I think this is not the cause of error, because I have deployed my software on hundreds of clients on Window 7,8,10)

*Note3- My Client is Italian that's why he has set Italian Language on his Laptop From Windows Settings, that's why all System icons and Text appears in Italian on his laptop. But 3rd party software appear in same language in which that software intend to. My software's Language is English and all the labels and buttons appears in English in my Software and this is perfectly fine.
Only the problem is with JFormatedTextField as I mentioned above.
So I am thing about may be there is such a system setting which is cause of this change/Error on his Laptop.
Waiting for your guidance. Thanks in advance.

Dani AI

Generated

As and pointed out, the JFormattedTextField behavior is locale-driven: Java uses the platform/JVM default Locale when it formats numbers, and many European locales (including Italian) use a comma as the decimal separator. The field is working as designed; the question is whether to accept that localization or force a specific format for the application.

Quick checks (run inside the app) to confirm what the JVM sees:

System.out.println("Locale.getDefault(): " + Locale.getDefault());
System.out.println("decimalSeparator: " + DecimalFormatSymbols.getInstance().getDecimalSeparator());

Two practical approaches:

  • Restrict formatting per field (recommended): build a NumberFormatter/DecimalFormat with an explicit Locale or DecimalFormatSymbols and attach it to each JFormattedTextField so the rest of the app is unaffected.
NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
NumberFormatter fmt = new NumberFormatter(nf);
fmt.setAllowsInvalid(false);
JFormattedTextField field = new JFormattedTextField(fmt);
  • Accept both comma and dot from users: normalize the typed string before parsing (handle grouping separators carefully). Example: remove grouping characters, treat the last comma/dot as the decimal separator, replace it by '.' and parse. Use a custom NumberFormatter subclass that overrides stringToValue to normalize input. This is useful when users may type either separator.

Avoid changing the global locale (Locale.setDefault) unless the entire application should follow a different locale; that affects dates, currency, messages, etc. See the Swing API and DecimalFormatSymbols for details: JFormattedTextField and DecimalFormatSymbols.

As suggested, helper utilities are useful—implement a small utility that returns a configured NumberFormatter or a normalizer method and reuse it across fields.

Recommended Answers

All 3 Replies

Actually this is how it should work on Italian systems.

Use a comma instead of a decimal point to show decimals in Italian.

0,5 (zero virgola cinque) 0.5 (nought point five)
3,4 (tre virgola quattro) 3.4 (three point four)

My view is this is not broken. But there is more to read about this with https://www.google.com/search?&q=force+decimal+point+in+italian+window

Some countries including France where I live use a . between thousands and a , before the decimals. Java localisation will automatically default to display numbers in the correct format for the users locale.
You can override this by setting an explicit locale in your program if you want to use the same formatting regardless of local norms.

https://docs.oracle.com/javase/6/docs/api/java/util/Locale.html#setDefault(java.util.Locale)

In this case, I would recommend you to use various handy helpers, perhaps on the Internet you can find information that will help you to solve this problem. I just often do it myself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.