• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with giving validation in Jtextfield

 
Jahangir Alam
Greenhorn
Posts: 15
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I created a jtextfield for Student ID. I want to give validation on the data of this field that the id must be of 5 char long. So i set focuslost listener so that when I will move from this field it would show a message if the id is not valid.

Here is my code :



The problem is that if the length is not 5, it shows more than one message but I need only one. How to fix it?
Any help would be great for me!
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe the problem is the grabFocus() method. In general you should be using requestFocusInWindow() to request focus on a component.

However, you should NOT be using a FocusListener for this.

Instead you should be using an InputVerifier. It will handle all this code for you. Read the API for a working example.
 
Jahangir Alam
Greenhorn
Posts: 15
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Rob!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!