Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
705 views
in Technique[技术] by (71.8m points)

java - how to make jtextfield only accept characters in netbeans

I have done a sample project in netbeans of registration. In the jtextfield1 is the user id and Jtextfiled7 is country, Both must in characters not in numeric or not allowed spaces and special characters.how it is possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use JFormattedTextField or you can code inside the JTextField's KeyTyped event

jTextField.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyTyped(java.awt.event.KeyEvent evt) {

         if(!(Character.isLetter(evt.getKeyChar()))){
                evt.consume();
            }
        }
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...