I have a code here that i got form MDP's weblog. the sizefilter and the number filter. how do i make a textfield set its filter for two document filters.
Here isthe numberfilter
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);
}
}
this code is for the 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();
}
}
You've got two options as far as I can see. Either create a composite filter which iterates over each filter:
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);
}
}
}
You'd probably want to instantiate the composite with the more restrictive filter first, so you'd construct it like so:
new CompositeFilter(new SizeFilter(10), new IntFilter());
If order is critically important, you might consider rewriting your filters as decorators, e.g. pass the second filter into the first and then call it.
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();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With