Vivek
Vivek

Reputation: 27

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?

Upvotes: 1

Views: 14550

Answers (3)

user5511875
user5511875

Reputation:

@Vivek ,

I have the answer for this question you asked. please do as per following instructions.

In Netbeans right click on JTextfield and select the events>>key>>key typed and enter the following code between the codes. the following is the code for it for only accept characters

enter code here
char c=evt.getKeyChar();

    if(!(Character.isAlphabetic(c) ||  (c==KeyEvent.VK_BACKSPACE)||  c==KeyEvent.VK_DELETE ))
        evt.consume();

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347334

For real time validation, use a DocumentFilter, see and Implementing a Document Filter and DocumentFilter Examples for more details.

Have a look at:

for more examples.

For post validation, see Validating Input for more details

Upvotes: 1

Lahiru Jayathilake
Lahiru Jayathilake

Reputation: 601

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();
            }
        }
    });

Upvotes: 1

Related Questions