Jtexfield limitar tamanho

0 respostas
bacoco

olá,vi aqui no forum como limitar um campo jtextfield e segui a dica criei a classe : FixedLengthDocument

Da seguinte maneira
package preventivaapp;

import javax.swing.text.AttributeSet;  
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;  
      
    public class FixedLengthDocument extends PlainDocument {  
        private int maxLength;  
      
        public FixedLengthDocument(int maxlen) {  
            super();  
              
            if (maxlen <= 0) {
                throw new IllegalArgumentException("You must specify a maximum length!");
            }  
              
            maxLength = maxlen;  
        }  
      
        @Override  
        public void insertString(int offset, String str, AttributeSet attr)   
        throws BadLocationException {  
            if (str == null || getLength() == maxLength) {
                return;
            }  
      
            int totalLen = (getLength() + str.length());  
            if (totalLen <= maxLength) {  
                super.insertString(offset, str, attr);  
                return;  
            }  
              
            String newStr = str.substring(0, (maxLength - getLength()));  
            super.insertString(offset, newStr, attr);  
        }  
    }

Pois bem,em outro package da aplicação estou tentando chama-la da seguinte maneria

edtMatricula.setDocument(preventivaapp.FixedLengthDocument(8));

edtMatricula é o meu JTextField

Em qual parte do codigo do meu JFrame devo colocar essa chamada e como devo faze-la?

Obrigado

Criado 3 de janeiro de 2013
Respostas 0
Participantes 1