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?
Asked
Active
Viewed 1.4k times
1
-
2[This question](http://stackoverflow.com/questions/5662651/how-to-implement-in-java-jtextfield-class-to-allow-entering-only-digits)'s answers will show you the right approach, although you'll need to modify it (since it's doing basically the opposite of what you want). – resueman Dec 20 '15 at 03:20
-
Have a look at [Implementing a Document Filter](http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html#filter) and [DocumentFilter Examples](http://www.jroller.com/dpmihai/entry/documentfilter) – MadProgrammer Dec 20 '15 at 03:23
-
1You could also have a look at [Validating Input](http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html#inputVerification) – MadProgrammer Dec 20 '15 at 03:24
-
I was making a small swing application that needed to validate a `JTextField`s input a couple of weeks ago. I found the question linked in the first comment and implemented the code and it worked but I wanted to do more than what it offered, so another search lead me to MadProgrammer's Validating Input link and it offered everything I wanted. I'd suggest implementing the former for a quick solution and the ladder for an in-depth solution. – Jonny Henly Dec 20 '15 at 03:40
-
Here is an [example](http://stackoverflow.com/questions/34371647/can-not-write-editable-jcombobox/34372342#34372342) with a document filter I gave today. – user1803551 Dec 20 '15 at 10:43
3 Answers
1
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();
}
}
});

Lahiru Jayathilake
- 601
- 2
- 10
- 28
-
2No, no, no. It is never appropriate to use a KeyListener with a text component, instead, use a DocumentFilter, it's what's it designed for – MadProgrammer Dec 20 '15 at 04:45
1
For real time validation, use a DocumentFilter
, see and Implementing a Document Filter and DocumentFilter Examples for more details.
Have a look at:
- JTextField limiting character amount input and accepting numeric only
- How to set DocumentFilter with input length and range? e.g. 1-3 or 10-80
for more examples.
For post validation, see Validating Input for more details

Community
- 1
- 1

MadProgrammer
- 343,457
- 22
- 230
- 366
0
@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();