Olá!
Gostaria de permitir espaços no jTextField que criei. Sou completamente iniciante, mas consegui bastante coisa no site gui.
Meu problema é que no bloqueio de letras eu preciso que ele não bloquei também espaços. Segue o meu código:
//Form pra bloquear letra Inicio
public class Limitar_Letras extends PlainDocument
{
private int iMaxLength;
public Limitar_Letras(int maxlen)
{
super();
iMaxLength = maxlen;
}
@Override
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.replaceAll("[^aA-zZ]", “”), attr);
else
{
if (getLength() == iMaxLength)
return; // nada a fazer
String newStr = str.substring(0, (iMaxLength - getLength()));
super.insertString(offset, newStr, attr);
}
}
}
//Fim
Tentei fazer assim, mas não funcionou:
super.insertString(offset, str.replaceAll("[^ -zZ]", “”), attr); //com espaço antes do -
super.insertString(offset, str.replaceAll("[^SPACE-zZ]", “”), attr); //Nome da tecla
super.insertString(offset, str.replaceAll("[^""-zZ]", “”), attr); //Não aceita o vazio
super.insertString(offset, str.replaceAll("[^null-zZ]", “”), attr); //Null fica como caractere
Estou sem saída, não sei mais o que tentar. Como sou iniciante total este foi o único código que consegui entender até agora e não sei se é possível fazer por ele.
Desde já, obrigado a todos pelo prazer em ajudar. Espero um dia poder contribuir.