Created
October 20, 2015 10:23
-
-
Save altitdb/b98ccd20f2ca6e739cff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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