JFormattedTextField with strict parsing number format

旧巷老猫 提交于 2019-12-11 13:44:11

问题


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:

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

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".

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

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?

Test Code:

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

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


回答1:


Here is my solution:

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

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


来源:https://stackoverflow.com/questions/33808144/jformattedtextfield-with-strict-parsing-number-format

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