Limitar numero de caracteres em um Jtable

0 respostas
L

Senhores;

Estou precisando limitar o número de caracteres digitado em um JtextField, sendo que o mesmo esta vinculado a uma Jtable. Se eu tiro o vinculo o limitador funciona.

Classe utilizada:

import javax.swing.text.*;

public class FixedLengthDocument extends PlainDocument {

private int iMaxLength;   

public FixedLengthDocument(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, attr);   // ...aceita str  
    
    else  
      {   
    if (getLength() == iMaxLength) return; // nada a fazer   
    String newStr = str.substring(0, (iMaxLength - getLength()));   

    super.insertString(offset, newStr, attr);   
      }   

    }

}

Criado 2 de agosto de 2011
Respostas 0
Participantes 1