【问题标题】:JSpinner and double with dot or comma as separatorJSpinner 和 double 用点或逗号作为分隔符
【发布时间】:2015-08-31 13:50:13
【问题描述】:

我想允许“逗号”和“点”作为双精度分隔符。

我可以在字符串中使用替换方法来只获取一个分隔符,但问题是双值是 JSpinner 的值,我找不到任何方法来允许这两个分隔符。例如,如果我将语言环境设置为法语,则只允许使用一个分隔符。

【问题讨论】:

  • 如何在单个 JSpinner 中同时使用两个分隔符?
  • easy just 10,02 将与 10.02 相同

标签: java double locale separator jspinner


【解决方案1】:

只需对DefaultEditorJFormattedTextFieldJSpinner 使用自定义格式化程序,如下面的代码:

import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.JFormattedTextField.AbstractFormatter;
import javax.swing.JFormattedTextField.AbstractFormatterFactory;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.JSpinner.DefaultEditor;
import javax.swing.SpinnerNumberModel;

public class Main {
    public static class CommmaDotFormatter extends AbstractFormatter {
        private boolean useComma = false;

        @Override
        public Object stringToValue(String text) throws ParseException {
            if (text == null || text.trim().isEmpty())
                throw new ParseException("Null or empty text.", 0);
            try {
                useComma = (text.contains(",") && (text.indexOf(',') == text.lastIndexOf(',')));
                return Double.valueOf(useComma? text.replace(',', '.'): text);
            }
            catch (final NumberFormatException nfx) {
                //Find the location of the error (so as to generate appropriate ParseException):
                int i = 0;
                for (final int cp: text.codePoints().toArray()) {
                    if (!Character.isDigit(cp) && cp != ',' && cp != '.')
                        throw new ParseException("Failed to parse text \"" + text + "\".", i);
                    ++i;
                }
                //Could happen if the text is composed by digits and more than one dot/comma:
                throw new ParseException("Failed to parse text \"" + text + "\".", 0);
            }
        }

        @Override
        public String valueToString(final Object value) throws ParseException {
            final String text = String.format("%.2f", value);
            return useComma? text: text.replace(',', '.');
        }
    }

    public static class CommmaDotFormatterFactory extends AbstractFormatterFactory {
        @Override
        public AbstractFormatter getFormatter(final JFormattedTextField tf) {
            if (!(tf.getFormatter() instanceof CommmaDotFormatter))
                return new CommmaDotFormatter();
            return tf.getFormatter();
        }
    }

    public static void main(final String[] args) {
        final JSpinner spin = new JSpinner(new SpinnerNumberModel(0, -10, 10, 0.01));

        ((DefaultEditor) spin.getEditor()).getTextField().setFormatterFactory(new CommmaDotFormatterFactory());

        final JFrame frame = new JFrame("JSpinner infinite value");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(spin);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

注意:属性useComma 可以省略。它只是为了维护用户最后输入的状态而存在。例如,如果用户使用逗号而不是点输入值,则微调器将继续使用逗号旋转。点也一样。无论用户类型如何,都将保留为将来的值,当然,他/她可以随时再次更改。但是即使没有此属性,您的问题也可以得到满足:每次将 stringToValue 中的给定文本解析为 double 之前,您只需执行 text.replace(',', '.')

【讨论】:

    猜你喜欢
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多