文档过滤器在 Java 中不起作用?

发布于 2024-12-06 12:19:10 字数 354 浏览 3 评论 0原文

在超过 10 个字符的文本字段中,它必须显示错误。为此,我使用了文档过滤器:

JTextField field = (JTextField) txtFld;
AbstractDocument document = (AbstractDocument) field.getDocument();
document.setDocumentFilter(new DocumentSizeAndUppercaseFilter(10));

所以这是我的文档过滤器编码。我通过文档过滤器注册了文本字段。但这里什么也没发生。如何使用文档过滤器?

DocumentSizeAndUppercaseFilter 类有错误消息。

In text field more than 10 characters na, it has to show an error. For that i used document filter:

JTextField field = (JTextField) txtFld;
AbstractDocument document = (AbstractDocument) field.getDocument();
document.setDocumentFilter(new DocumentSizeAndUppercaseFilter(10));

So this is my document filter coding. I registered the textfield through document filter. But nothing has happening here. How to use document filter?

DocumentSizeAndUppercaseFilter class which has the error msg.

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

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

发布评论

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

评论(2

感性不性感 2024-12-13 12:19:10

如果没有看到 DocumentSizeAndUppercaseFilter 的代码,我怀疑您没有实现(/覆盖)DocumentFilter替换方法:

@Override
public void replace(DocumentFilter.FilterBypass fb, int offset,
                    int length, String text, AttributeSet attrs)
        throws BadLocationException {

    ....
}

以下代码的屏幕截图:

screenshot


DocumentSizeAndUppercaseFilter 的示例实现:

static class DocumentSizeAndUppercaseFilter extends DocumentFilter {

    private final int max;

    public DocumentSizeAndUppercaseFilter(int max) {
        this.max = max;
    }

    @Override
    public void insertString(DocumentFilter.FilterBypass fb, int offset,
                             String text, AttributeSet attr) 
            throws BadLocationException {
        if (fb.getDocument().getLength() + text.length() < max)
            super.insertString(fb, offset, text.toUpperCase(), attr);
        else 
            showError();
    }

    @Override
    public void replace(DocumentFilter.FilterBypass fb, int offset,
                        int length, String text, AttributeSet attrs)
            throws BadLocationException {
        int documentLength = fb.getDocument().getLength();
        if (documentLength - length + text.length() < max)
            super.replace(fb, offset, length, text.toUpperCase(), attrs);
        else 
            showError();
    }

    private void showError() {
        JOptionPane.showMessageDialog(null, "Too many characters entered");
    }
}

示例 main:

public static void main(String[] args) {

    JTextField firstName = new JTextField();
    AbstractDocument d = (AbstractDocument) firstName.getDocument();
    d.setDocumentFilter(new DocumentSizeAndUppercaseFilter(10));

    JFrame frame = new JFrame("Test");
    frame.add(firstName);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 60);
    frame.setVisible(true);
}

Without seeing the code for DocumentSizeAndUppercaseFilter I would suspect that you havn't implemented (/override) the DocumentFilter's replace method:

@Override
public void replace(DocumentFilter.FilterBypass fb, int offset,
                    int length, String text, AttributeSet attrs)
        throws BadLocationException {

    ....
}

Screenshot from the code below:

screenshot


Example implementation of DocumentSizeAndUppercaseFilter:

static class DocumentSizeAndUppercaseFilter extends DocumentFilter {

    private final int max;

    public DocumentSizeAndUppercaseFilter(int max) {
        this.max = max;
    }

    @Override
    public void insertString(DocumentFilter.FilterBypass fb, int offset,
                             String text, AttributeSet attr) 
            throws BadLocationException {
        if (fb.getDocument().getLength() + text.length() < max)
            super.insertString(fb, offset, text.toUpperCase(), attr);
        else 
            showError();
    }

    @Override
    public void replace(DocumentFilter.FilterBypass fb, int offset,
                        int length, String text, AttributeSet attrs)
            throws BadLocationException {
        int documentLength = fb.getDocument().getLength();
        if (documentLength - length + text.length() < max)
            super.replace(fb, offset, length, text.toUpperCase(), attrs);
        else 
            showError();
    }

    private void showError() {
        JOptionPane.showMessageDialog(null, "Too many characters entered");
    }
}

Example main:

public static void main(String[] args) {

    JTextField firstName = new JTextField();
    AbstractDocument d = (AbstractDocument) firstName.getDocument();
    d.setDocumentFilter(new DocumentSizeAndUppercaseFilter(10));

    JFrame frame = new JFrame("Test");
    frame.add(firstName);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 60);
    frame.setVisible(true);
}
極樂鬼 2024-12-13 12:19:10

从简单的事情开始。

Swing 教程中关于实现文档过滤器的部分有一个工作示例可以完成您想要的一半工作。

Start with something simple.

The section from the Swing tutorial on Implementing a Document Filter has a working example that does half of what you want.

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