Remover não numéricos jtextfield

1 resposta
marceloamigo

Boa tarde pessoal!
Estou com problemas no seguinte código

public static String removeStrings(String str){
        StringBuffer tratado = new StringBuffer();
        Pattern numericos = Pattern.compile("[0-9]", Pattern.CASE_INSENSITIVE);
        Matcher resComparacao = numericos.matcher(str);
        while (resComparacao.find()) {
            tratado.append(resComparacao.group());
        }       
        return tratado.toString();       
    }

e

private void txtValidadePropertyChange(java.beans.PropertyChangeEvent evt) {
        txtValidade.getDocument().addDocumentListener(new DocumentListener() {
            public void changedUpdate(DocumentEvent e) {
                txtValidade.setText(Uteis.removeStrings(txtValidade.getText()));
            }
            public void removeUpdate(DocumentEvent e) {
                txtValidade.setText(Uteis.removeStrings(txtValidade.getText()));
            }
            public void insertUpdate(DocumentEvent e) {
                txtValidade.setText(Uteis.removeStrings(txtValidade.getText()));
            }
        });
}

erro: java.lang.IllegalStateException: Attempt to mutate in notification

alguma dica??

1 Resposta

davidtiagoconceicao

Você não pode usar do setText o JTextField dentro do listener de PropertyChange, já que uma mudança no texto dispararia outro evento de PropertyChange, que poderia tentar alterar o texto novamente, que dispararia outro evento de PropertyChange e assim sucessivamente até o estouro da pilha.

Criado 27 de fevereiro de 2009
Ultima resposta 27 de fev. de 2009
Respostas 1
Participantes 2