JTextField limit input to certains chars only

后端 未结 2 1298
天命终不由人
天命终不由人 2021-01-19 02:09

i\'d like to create JTextField with input characters limited to someting like \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYWZZ0123456789+&@#/%?=~_-|!:,.;\" so i t

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-19 03:16

    Try a JFormattedTextField and use

    MaskFormatter mf = new MaskFormatter();
    mf.setValidCharacters("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYWZZ0123456789+&@#/%?=~_-|!:,.;");
    JFormattedTextField textField = new JFormattedTextField(mf);
    

    Edit: Sorry, that was the wrong code, here's the working one

提交回复
热议问题