JSpinner editor locale

房东的猫 提交于 2019-12-02 10:16:48

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