• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with giving validation in Jtextfield

 
Greenhorn
Posts: 15
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Rob!
 
reply
    Bookmark Topic Watch Topic
  • New Topic