【问题标题】:JFormattedTextField using a regular expression formatter?JFormattedTextField 使用正则表达式格式化程序?
【发布时间】:2010-02-11 02:03:41
【问题描述】:

在让 JFormattedTextField 与我的自定义格式一起使用感到非常沮丧之后,我想知道是否有使用正则表达式的 FormatterFormatterFactory

我的想法是,如果有,那么我可以将它包装在一个静态类中并像这样调用它:

mFormattedTextField.setFormatterFactory(
    SomeStaticClass.getRegexFormatFactory("^(\\d{1,}h)(\\s([0-5])?[0-9]m)?$"));

查看我的previous question 了解更多背景信息:
" 我想使用 JFormattedTextField 允许用户将持续时间值输入到表单中。示例有效值是:2h 30m 72h 15m6h0h"

【问题讨论】:

标签: java regex time jformattedtextfield


【解决方案1】:

你读过this article吗?万一链接失效了,它说明你真正需要做的就是覆盖 AbstractFormatter 的 stringToValue 方法,就像这样:

public Object stringToValue(String text) throws ParseException {
    Pattern pattern = getPattern();

    if (pattern != null) {
        Matcher matcher = pattern.matcher(text);

        if (matcher.matches()) {
            return super.stringToValue(text);
        }
        throw new ParseException("Pattern did not match", 0);
    }
    return text;
}

实际上,快速搜索会产生几个完全实现的免费解决方案;这些都不足以满足您的需求吗?

【讨论】:

    猜你喜欢
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多