How to limit the number of digits in a JPasswordField in Java?

后端 未结 4 787
轻奢々
轻奢々 2020-12-12 01:20

I have my Java code working in Eclipse but I need to add a few functionnality.

First, how to limit the number of digits that can be entered by the user ? Actually I

4条回答
  •  不知归路
    2020-12-12 01:48

    JPasswordField limit characters to 4

    private void edtPinCodeKeyReleased(java.awt.event.KeyEvent evt) {                                       
        if (String.valueOf(edtPinCode.getPassword()).trim().length() > 4) 
            edtPinCode.setText(String.valueOf(edtPinCode.getPassword()).trim().substring(0, 4));
    } 
    

提交回复
热议问题