JTextField terminal input for character certification only

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+&@#/%?=~_-|!:,.;";

//... my class body ...//

@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?

+5
source share
2 answers

JFormattedTextField

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

: , ,

+3

All Articles