JSpinner editor locale

前端 未结 1 862
广开言路
广开言路 2021-01-26 07:26

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_

相关标签:
1条回答
  • 2021-01-26 07:50

    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);
    
    0 讨论(0)
提交回复
热议问题