If you could give me a clue or link I would be grateful, thanks :)
How about Text Component Features - Implementing a Document Filter?
To implement a document filter, create a subclass of
DocumentFilter
and then attach it to a document using thesetDocumentFilter
method defined in theAbstractDocument
class.
This might help:
public class NoAlphaNumFilter extends DocumentFilter {
String notAllowed = "[A-Za-z0-9]";
Pattern notAllowedPattern = Pattern.compile(notAllowed);
public void replace(FilterBypass fb, int offs,
int length,
String str, AttributeSet a)
throws BadLocationException {
super.replace(fb, offs, len, "", a); // clear the deleted portion
char[] chars = str.toCharArray();
for (char c : chars) {
if (notAllowedPattern.matcher(Character.toString(c)).matches()) {
// not allowed; advance counter
offs++;
} else {
// allowed
super.replace(fb, offs++, 0, Character.toString(c), a);
}
}
}
}
To apply this to a JTextField
:
((AbstractDocument) myTextField.getDocument()).setDocumentFilter(
new NoAlphaNumFilter());