JPasswordFiel - setColumn não está limitando tamanho da senha

Como faço para limitar o número de caracteres de um JPasswordField? No construtor estou setando o column dele mas não está adiantando.

    public static String getPass() {
 
      String p1, p2, pass = null;
      JPasswordField pwd = new JPasswordField(8);
      Object[] message = {"Digite uma senha para o usuário", pwd};

      int resposta = JOptionPane.showConfirmDialog(null, message, "Senha de usuário", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
      if (resposta == JOptionPane.OK_OPTION) {
         p1 = String.valueOf(pwd.getPassword());
         Object[] message2 = {"Confirme a senha digitada", pwd};
         pwd.setText("");
         resposta = JOptionPane.showConfirmDialog(null, message2, "Senha de usuário", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
         if (resposta == JOptionPane.OK_OPTION) {
            p2 = String.valueOf(pwd.getPassword());
            if (!p1.equals(p2)) {
               Messages.setException("As senhas não coincidem", "Erro de senha");
            } else {
               pass = p1;
            }
         } else {
            pass = null;
         }
      } else {
         pass = null;
      }

      return pass;
   }  

Já tentaste usar Document para ver se funciona?

Abraços!

Não. Como uso isso?

De uma olhada em:

http://imasters.uol.com.br/artigo/2364/java/formatar_campo_para_delimitar_a_quantidade_de_caracteres_e_ou_tipo_ace/

Valeu.

Cria essa classe no teu projeto:

[code]import javax.swing.text.*;

/**
*

  • @author AJFILHO
    */

public class TamanhoJTextField extends PlainDocument {

private int iMaxLength;

public TamanhoJTextField(int maxlen) {
    super();
    iMaxLength = maxlen;
}

@Override
public void insertString(int offset, String str, AttributeSet attr)
        throws BadLocationException {
    if (str == null) {
        return;
    }
    if (iMaxLength <= 0) 
    {
        super.insertString(offset, str, attr);
        return;
    }
    int ilen = (getLength() + str.length());
    if (ilen <= iMaxLength) 
    {
        super.insertString(offset, str, attr);
   }
}

}[/code]

Em seguida, depois de instanciado seu JPasswordField, você digita:

Abraços!

Já tentou fazer dessa otura forma?

[code]
codigoField.addKeyListener(new KeyAdapter() {
if(SEUCAMPO.getText().length() >= 8){
arg0.consume();
}
}
});

Oi,

Déjà vu: http://www.guj.com.br/posts/list/23711.java

Tchauzin!