I would like to create a JTextField with input characters limited to "? AbcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYWZZ0123456789 + & @ # /% = ~ _- |:!.,;" so I tried to override
public class CustomJTextField extends JTextField {
String goodchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYWZZ0123456789+&@#/%?=~_-|!:,.;";
@Override
public void processKeyEvent(KeyEvent ev) {
if(c != '\b' && goodchars.indexOf(c) == -1 ) {
ev.consume();
return;
}
else
super.processKeyEvent(ev);}}
but this is not what i want, because the user can no longer ctrl-c ctrl-v ctrl-x ... so I added
&& ev.getKeyCode() != 17 && ev.getKeyCode() !=67 && ev.getKeyCode() != 86 && ev.getKeyCode() !=0 &&
to the if condition, but now the user can insert unacceptable input, i.e. '(' or '<', without any problems ... what can I do?
source
share