Galera :?: :?: :?: :?: :?: :?:
minhas duas classe funcionam perfeitamente as duas só que agora quero melhorar meu código quero limitar a quantidade de caracteres digitados e verificar o tipo que esta sendo digitado proibindo números inteiros
Se alguém puder me ajudar eu agradeço :lol:
package logica.jtext;
import javax.swing.*;
import javax.swing.text.*;
public class FixedLengthDocument extends PlainDocument
{
private int iMaxLength;
public FixedLengthDocument(int maxlen)
{
super();
iMaxLength = maxlen;
}
public void insertString(int offset, String str, AttributeSet attr)
throws BadLocationException
{
if (str == null)
return;
if (iMaxLength <= 0) // aceitara qualquer no. de caracteres
{
super.insertString(offset, str, attr);
return;
}
int ilen = (getLength() + str.length());
if (ilen <= iMaxLength) // se o comprimento final for menor...
super.insertString(offset, str, attr); // ...aceita str
else
{
if (getLength() == iMaxLength)
return; // nada a fazer
String newStr = str.substring(0, (iMaxLength - getLength()));
super.insertString(offset, newStr, attr);
}
}
}
está valida o tipo digitado :?:
package controle.valida;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
public class CustomPlainDocument extends PlainDocument{
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
String currentText = getText(0, getLength());
String beforeOffset = currentText.substring(0, offs);
String afterOffset = currentText.substring(offs, currentText.length());
String proposedResult = beforeOffset + str + afterOffset;
Pattern pattern = Pattern.compile("[a-zA-Z ]+");
Matcher matcher = pattern.matcher(proposedResult);
if (matcher.matches()) {
super.insertString(offs, str, a);
}
}
}