public final class IntDocument implements Document {
private Document document;
private IntDocument(Document aDocument) {
document = aDocument;
}
public void insertString(int someOffset, String someStr, AttributeSet anAttributeSet) throws BadLocationException {
String tempInsertString = "";
if (null != someStr) {
tempInsertString = someStr.replaceAll("\\D", "");
}
document.insertString(someOffset, tempInsertString, anAttributeSet);
}
public void addDocumentListener(DocumentListener someListener) {
document.addDocumentListener(someListener);
}
public void addUndoableEditListener(UndoableEditListener someListener) {
document.addUndoableEditListener(someListener);
}
public Position createPosition(int someOffs) throws BadLocationException {
return document.createPosition(someOffs);
}
public Element getDefaultRootElement() {
return document.getDefaultRootElement();
}
public Position getEndPosition() {
return document.getEndPosition();
}
public int getLength() {
return document.getLength();
}
public Object getProperty(Object someKey) {
return document.getProperty(someKey);
}
public Element[] getRootElements() {
return document.getRootElements();
}
public Position getStartPosition() {
return document.getStartPosition();
}
public void getText(int someOffset, int someLength, Segment someTxt) throws BadLocationException {
document.getText(someOffset, someLength, someTxt);
}
public String getText(int someOffset, int someLength) throws BadLocationException {
return document.getText(someOffset, someLength);
}
public void putProperty(Object someKey, Object someValue) {
document.putProperty(someKey, someValue);
}
public void remove(int someOffs, int someLen) throws BadLocationException {
document.remove(someOffs, someLen);
}
public void removeDocumentListener(DocumentListener someListener) {
document.removeDocumentListener(someListener);
}
public void removeUndoableEditListener(UndoableEditListener someListener) {
document.removeUndoableEditListener(someListener);
}
public void render(Runnable someR) {
document.render(someR);
}
}
// Anwendung:
JTextField field = new JTextField();
field.setDocument(new IntDocument(field.getDocument()));