具有严格解析数字格式的JFormattedTextField [英] JFormattedTextField with strict parsing number format

查看:136
本文介绍了具有严格解析数字格式的JFormattedTextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于有人标记了我的其他问题( NumberFormat解析不够严格 )错误地重复,并且没有删除重复的标签,尽管我指出这是不同的,但我将再次将问题和解决方案一起发布.也许对某人有帮助.我们开始:

Since someone marked my other question (NumberFormat parse not strict enough) as duplicate by mistake and did not remove the duplicate label although I pointed out that it was different, I will post the question again together with my solution. Maybe it is helpful to someone. Here we go:

我有一个带有Locale.US的NumberFormat的JFormattedTextField.因此,小数点分隔符是点,分组分隔符是逗号.

I have a JFormattedTextField with a NumberFormat with Locale.US. So the decimal separator is the point and the grouping separator is the comma.

现在,我在此文本字段中输入字符串"1,23",并将焦点移至另一个组件.我希望该字符串消失(就像我键入"a"而不是"1,23"时那样),因为使用Locale.US时,它显然不是数字的有效表示形式.而是将文本字段中的文本更改为"123".

Now I type the string "1,23" in this text field and move the focus to another component. I would expect the string to disappear (like it does when i type "a" instead of "1,23") because it is obviously not a valid representation of a number when using Locale.US. But instead the text in the text field is changed to "123".

这是因为解析时使用的NumberFormat并不严格,只是忽略了逗号.

This is because the used NumberFormat is not strict when parsing and simply ignores the comma.

问题:在这种情况下,如何告诉NumberFormat抛出ParseException,以便在将焦点移至另一个组件后文本字段为空?

Question: How can I tell NumberFormat to throw a ParseException in this case so the text field will be empty after moving the focus to another component?

测试代码:

    JDialog dialog = new JDialog();
    JPanel panel = new JPanel(new BorderLayout());
    dialog.getContentPane().add(panel);

    NumberFormat nf = NumberFormat.getInstance(Locale.US);
    JFormattedTextField textField = new JFormattedTextField(nf);
    textField.setText("1,23");
    panel.add(textField, BorderLayout.CENTER);
    panel.add(new JButton("focus"), BorderLayout.EAST);

    dialog.pack();
    dialog.setVisible(true);

将焦点从文本字段移至按钮,文本将变为"123".

Move the focus from the text field to the button and the text will change to "123".

推荐答案

这是我的解决方案:

首先,我创建一个带有严格解析的自定义DecimalFormat.仅允许数字和小数点后一位.

First I create a custom DecimalFormat with strict parsing. Only digits and at most one decimal point are allowed.

    public class StrictDecimalFormat extends DecimalFormat {

        private static final Pattern NUMBER_PATTERN = Pattern.compile("^\\d*[.]?\\d*$");

        public Number parse(String text, ParsePosition pos) {
            Matcher matcher = NUMBER_PATTERN.matcher(text);
            if (matcher.matches()) {
                return super.parse(text, pos);
            }
            return null;
        }
    }

现在,我将这种十进制格式用于JFormattedTextField.由于语言环境本身不能设置为十进制格式,因此我必须创建适当的DecimalFormatSymbols并将其设置.

Now I use this decimal format for my JFormattedTextField. Since the locale itself can not be set at the decimal format, I have to create the appropriate DecimalFormatSymbols and set it.

    DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(Locale.US);
    DecimalFormat strictFormat = new StrictDecimalFormat();
    strictFormat.setDecimalFormatSymbols(decimalFormatSymbols);

    JDialog dialog = new JDialog();
    JPanel panel = new JPanel(new BorderLayout());
    dialog.getContentPane().add(panel);

    JFormattedTextField textField = new JFormattedTextField(strictFormat);
    textField.setText("1,23");
    panel.add(textField, BorderLayout.CENTER);
    panel.add(new JButton("focus"), BorderLayout.EAST);

    dialog.pack();
    dialog.setVisible(true);

这篇关于具有严格解析数字格式的JFormattedTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆