Question

I'm creating a JSpinner and setting a NumberEditor with a custom format.

But no matter what I do the format uses "." instead of ",", not according to my locale (pt_BR).

priceSpinner = new JSpinner();
priceSpinner.setEditor(new JSpinner.NumberEditor(priceSpinner, "0.00"));

Is it possible to use "," instead of "." as a decimal separator?

Was it helpful?

Solution

By specifying a custom format pattern you are telling the JRE to ignore your locale and use that specific format. If you are simply after a spinner for numbers to two decimal places, use setModel() instead of setEditor() which will create a NumberEditor for you:

    JSpinner priceSpinner = new JSpinner();
    priceSpinner.setModel(new SpinnerNumberModel(0.00, 0.00, 100.00, 0.01));

If you absolutely must use your own format pattern, you can adjust the decimal format symbols for that pattern after you've created it:

    JSpinner priceSpinner = new JSpinner();
    JSpinner.NumberEditor editor = new JSpinner.NumberEditor(priceSpinner, "0.00");
    DecimalFormat format = editor.getFormat();
    //better to use Locale.getDefault() here if your locale is already pt_BR
    Locale myLocale = new Locale("pt", "BR");
    format.setDecimalFormatSymbols(new DecimalFormatSymbols(myLocale));
    priceSpinner.setEditor(editor);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top