I just saw some questions here, but none of them resolve the problem for me. I have got a JPasswordField
and I need that the user cannot input more than 4 digits in the EscPin JPasswordField
private void EscPinActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private javax.swing.JButton Entrar;
private javax.swing.JTextField EscNConta;
private javax.swing.JPasswordField EscPin;
private javax.swing.JLabel Logo;
private javax.swing.JLabel NConta;
private javax.swing.JLabel Pin;
private javax.swing.JButton Sair;
You have to get the internal Document of JPasswordField. try this it will work.
PlainDocument d=(PlainDocument) jPasswordField1.getDocument();
d.setDocumentFilter(new DocumentFilter(){
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
String string =fb.getDocument().getText(0, fb.getDocument().getLength())+text;
if(string.length() <= 4)
super.replace(fb, offset, length, text, attrs); //To change body of generated methods, choose Tools | Templates.
}
});