1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| textfield = new JTextField();
((PlainDocument)textfield.getDocument()).setDocumentFilter(new DocumentFilter() {
@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
if(string == null) {
return;
} else{
replace(fb, offset, 0, string, attr); // on renvoie l'insertion de caractère sur le remplacement pour traiter tous les cas une fois de la même manière
}
}
@Override
public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
replace(fb, offset, length, "", null); // on renvoie l'insertion de caractère sur le remplacement pour traiter tous les cas une fois de la même manière
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
Document doc = fb.getDocument();
// on ne peut modifier que ce qui se trouve à la fin du texte saisi
if ( offset==doc.getLength() ) {
super.replace(fb, offset, length, text, attrs);
}
}
}); |
Partager