JSpinner editor locale

谁都会走 提交于 2019-12-20 06:20:21

问题


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:


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);


来源:https://stackoverflow.com/questions/1035063/jspinner-editor-locale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!