5

我在这里有一个代码,我从 MDP 的博客中获得。大小过滤器和数字过滤器。我如何让一个文本字段为两个文档过滤器设置它的过滤器。

这是数字过滤器

import javax.swing.text.BadLocationException;
import javax.swing.text.AttributeSet;
import javax.swing.text.DocumentFilter;

public class IntFilter extends DocumentFilter {

public void insertString(DocumentFilter.FilterBypass fb, int offset,
                         String string, AttributeSet attr)
        throws BadLocationException {

    StringBuffer buffer = new StringBuffer(string);
    for (int i = buffer.length() - 1; i >= 0; i--) {
        char ch = buffer.charAt(i);
        if (!Character.isDigit(ch)) {
            buffer.deleteCharAt(i);
        }
    }
    super.insertString(fb, offset, buffer.toString(), attr);
}

public void replace(DocumentFilter.FilterBypass fb,
                    int offset, int length, String string, AttributeSet attr) throws BadLocationException {
    if (length > 0) fb.remove(offset, length);
    insertString(fb, offset, string, attr);
}
}

此代码适用于 sizefilter

import java.awt.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class SizeFilter extends DocumentFilter {

private int maxCharacters;    

public SizeFilter(int maxChars) {
    maxCharacters = maxChars;
}

public void insertString(FilterBypass fb, int offs, String str, AttributeSet a)
        throws BadLocationException {

    if ((fb.getDocument().getLength() + str.length()) <= maxCharacters)
        super.insertString(fb, offs, str, a);
    else
        Toolkit.getDefaultToolkit().beep();
}

public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)
        throws BadLocationException {

    if ((fb.getDocument().getLength() + str.length()
            - length) <= maxCharacters)
        super.replace(fb, offs, length, str, a);
    else
        Toolkit.getDefaultToolkit().beep();
}
}
4

1 回答 1

2

据我所知,你有两个选择。要么创建一个迭代每个过滤器的复合过滤器:

public class CompositeFilter extends DocumentFilter {
    private final DocumentFilter[] filters;

    public CompositeFilter(DocumentFilter... filters) {
        this.filters = filters;
    }

    public void insertString(FilterBypass fb, int offs, String str, AttributeSet a)
        throws BadLocationException {
        for (DocumentFilter filter : filters) {
            filter.insertString(fb, offs, str, a);
        }
    }

    public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)
        throws BadLocationException {
        for (DocumentFilter filter : filters) {
            filter.replace(fb, offs, length, a);
        }
    }
}

您可能希望首先使用限制性更强的过滤器来实例化复合材料,因此您可以像这样构造它:

new CompositeFilter(new SizeFilter(10), new IntFilter());

如果顺序非常重要,您可以考虑将过滤器重写为装饰器,例如将第二个过滤器传递给第一个过滤器,然后调用它。

public class SizeFilter extends DocumentFilter {
    private int maxCharacters;    
    private final DocumentFilter delegate;

    public SizeFilter(int maxChars, DocumentFilter delegate) {
        maxCharacters = maxChars;
        this.delegate = delegate;
    }

    public void insertString(FilterBypass fb, int offs, String str, AttributeSet a)
        throws BadLocationException {

        if ((fb.getDocument().getLength() + str.length()) <= maxCharacters)
            delegate.insertString(fb, offs, str, a);
        else
            Toolkit.getDefaultToolkit().beep();
    }

    public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)
        throws BadLocationException {

        if ((fb.getDocument().getLength() + str.length() - length) <= maxCharacters)
            delegate.replace(fb, offs, length, str, a);
        else
            Toolkit.getDefaultToolkit().beep();
        }
    }
}
于 2012-10-12T13:34:41.773 回答