Gente axei aqui no guj a alguns dias uma classe que formataria um campo de texto para CEP
tentei seguir o padrão e fazer uma para CPF, mas não funcionou, compilou legal, não gera exceção e a classe do CPF não funciona a do CEP sim
codigo de ambas:
CEP (funciona)
package Classes;
import javax.swing.JOptionPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
public class DocumentoCep extends javax.swing.text.PlainDocument implements javax.swing.text.Document {
public static final long serialVersionUID = 1L;
public static final String NUMEROS = "[0-9]";
public static final short TAMANHO = 8;
private int tam;
private String num;
/**
Construtor para instanciar um vazio!
*/
public DocumentoCep(short tam, String num) {
this.tam = tam;
this.num = num;
}
/**
Construtor para formatar campos de texto onde se passa o CEP
*/
public DocumentoCep () {
this(TAMANHO, NUMEROS);
}
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException{
try{
String texto = getText(0,getLength());
if(texto.length() > TAMANHO){
return;
}
String ins = str.replaceAll("-", "");
Integer.parseInt(str);
if (texto.length() + ins.length() > TAMANHO) {
ins = ins.substring(0, TAMANHO - texto.length());
}
super.insertString(offs, str, a);
if(getLength() > 4){
int i = getText(0, getLength()).indexOf("-");
if(i < 0){
super.insertString(5, "-", a);
}
else if(i == 5) {
return;
}
else{
super.remove(i, 1);
super.insertString(5, "-", a);
}
}
}
catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(null, "Apenas numeros no campo CEP");
}
}
}
CPF ( não funciona ):
package Classes;
import javax.print.attribute.AttributeSet;
import javax.swing.text.BadLocationException;
/**
*
* @author Guilherme Santos Souza
*/
public class DocumentoCpf extends javax.swing.text.PlainDocument implements javax.swing.text.Document{
public static final long serialVersionUID = 2L;
public static final String NUMEROS = "[0-9]";
public static final short TAMANHO = 13;
private short max;
private String chars;
public DocumentoCpf(short max, String chars){
this.chars = chars;
this.max = max;
}
public DocumentoCpf(){
this(TAMANHO, NUMEROS);
}
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException{
try{
String texto = getText(0, getLength());
if(texto.length() > TAMANHO){
return;
}
String ins = str.replaceAll(".", "");
Integer.parseInt(str);
if((texto.length() + ins.length()) > TAMANHO){
ins = ins.substring(0, TAMANHO - texto.length());
}
super.insertString(offs, str, (javax.swing.text.AttributeSet) a);
if(getLength() > 2){
int i = getText(0, getLength()).indexOf(".");
if(i < 0){
super.insertString(3, ".", (javax.swing.text.AttributeSet) a);
}
else if(i == 3){
return;
}
else{
super.remove(i, 1);
super.insertString(3, ".", (javax.swing.text.AttributeSet) a);
}
}
if(getLength() > 6){
int i = getText(0, getLength()).indexOf(".");
if(i < 0){
super.insertString(7, ".", (javax.swing.text.AttributeSet) a);
}
else if(i == 7){
return;
}
else{
super.remove(i, 1);
super.insertString(7, ".", (javax.swing.text.AttributeSet) a);
}
}
if(getLength() > 10){
int i = getText(0, getLength()).indexOf(".");
if(i < 0){
super.insertString(11, "-", (javax.swing.text.AttributeSet) a);
}
else if(i == 11){
return;
}
else{
super.remove(i, 1);
super.insertString(11, "-", (javax.swing.text.AttributeSet) a);
}
}
}
catch(NumberFormatException nfe){
nfe.printStackTrace();
}
}
}