Skip to content

Instantly share code, notes, and snippets.

@altitdb
Created October 20, 2015 10:23
Show Gist options
  • Save altitdb/b98ccd20f2ca6e739cff to your computer and use it in GitHub Desktop.
Save altitdb/b98ccd20f2ca6e739cff to your computer and use it in GitHub Desktop.
import java.util.regex.Pattern;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
/**
*
* @author Altieres de Matos
*/
public class DocumentRightLeft extends PlainDocument {
private final Integer maxCaracter;
Pattern pattern = Pattern.compile("[^0-9]");
public DocumentRightLeft(Integer maxCaracter) {
this.maxCaracter = maxCaracter;
}
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
Integer tamanho = getLength();
if (tamanho < maxCaracter) {
String texto = getText(0, getLength());
remove(0, getLength());
super.insertString(0, new StringBuilder(texto + str.replaceAll(pattern.toString(), "")).toString(), a);
} else {
super.insertString(0, "", a);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment