Bookmark Topic Watch Topic
  • New Topic

Limit JTextField number of characters

 
Wael Atef
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi all,
i want to limit the number of characters a JTextField can accept to maximum of 6 characters so that any characters typed by the user beyond these characters not shown in TextField. How can i petrform that.
 
Abhik Sarkar
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Report post to moderator
You can use a javax.swing.InputVerifier to validate your JTextField.
Here is an example:

[ June 16, 2003: Message edited by: Abhik Sarkar ]
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello,

I tried the code sample given above. But it's not working . I'm working with JDK 5. I've added this text field to a jframe. Here's the code


I am still able to type beyond 6 characters in the text field
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Soniya,
In future, please start a new thread for your questions. Also, this particular thread is 6 years old.

The InputVerifier is not the correct solution. InputVerifier only ensures that the component will not loose focus in case the validation fails. It does not enforce the input conditions as you want.
This question comes up quite a few times and has been answered before. Search the forums.
The ranch Swing FAQ also has an article on numeric text fields with sample code. You can adapt the code to suit your requirements.

    Bookmark Topic Watch Topic
  • New Topic