JFormattedTextField 使用正则表达式格式化程序?

发布于 2024-08-20 20:33:35 字数 621 浏览 5 评论 0原文

在让 JFormattedTextField 与我的自定义格式一起使用方面经历了很多挫折之后,我想知道是否有使用正则表达式的 FormatterFormatterFactory

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

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

请参阅我的 上一个问题了解更多背景信息:
" 我想使用 JFormattedTextField 允许用户将持续时间值输入到表单中。有效值示例为:2h 30m 72h 15m 6h 0h"

After much frustration with getting a JFormattedTextField to work with my custom formats, I was wondering if there was a Formatter or FormatterFactory that uses regular expressions?

My idea is that if there is one, then I could wrap it in a static class and invoke it like so:

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

See my previous question for more background:
" I want to use a JFormattedTextField to allow the user to input time duration values into a form. Sample valid values are: 2h 30m 72h 15m 6h 0h"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾铮苏瑾 2024-08-27 20:33:35

您读过这篇文章吗?万一该链接失效了,它说明了您真正需要做的就是重写 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;
}

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

Have you read this article? In case that link rots away, it says all you really need to do override AbstractFormatter's stringToValue method, like this:

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

Actually, a quick search yields several fully-implemented, free solutions; were none of those sufficient to your needs?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文