views:

324

answers:

1

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?

+1  A: 

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);
Spyder
Thanks, it worked. I really need the custom pattern because I want "1,00" instead of "1".
tuler