This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

TextField validation

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gurus,
I realize that the topic of text field validation has been addressed on multiple occasions on this message board, however none addressed the issues that I need addressed.
I need to be able to validate a text field when it loses focus. If the validation fails the control needs to retain focus. I do NOT want to validate the data upon every key stroke. The problems encountered when trying to implement this using the FocusListener are:
If a button is pressed on the same panel, the focus lost event occurs and the button pressed event occurs too.
If the text field is on a tabbed pane, and a different tab is clicked on, the tab still appears even though the text field still has focus.
Essentially I need a way to capture when a user is trying to leave the control and stop them if the input is invalid.
what do you guys think?
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sean,
One way is, add actionlistener to the particular textfield(when you press enter it works). Put your validation in actionlistener. If the input is invalid. Use requestFocus method to regain the focus to textfield.
textfield.requestFocus()
Inorder to get the input value of the textfield which is to be vaildated. Use
textfield.getText()

For you problem check whether u have added Focuslistener to the panel. you have to add FocusListener to the particular component to wish, you need to have.

 
Sean Scott
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajan, Thanks for the reply.
What you described is what I am doing. In addition though, I request focus when the control loses focus and its data fails validation.
The problem with this scheme is that if the user clicks on a different tab. The TextField requestsFocus (and gets it) but the other tab still comes to the front and obscures the tab that contains the tab with my TextField.
The other problem is that when the user clicks on a button. I also requestFocus again (and get it), but the buttons event still occurs. I dont want this to happen.
Basically the requirement is to not allow the user to do ANYTHING else, until what they have entered is valid. This includes clicking buttons, switching to another view (This is an MDI Swing application), switching to a different tab, pressing the tab button to go to the next field etc.
Thanks
-sean
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sean,
I think the only way to stop other events from happening would involve the SystemEventQueue. Look at java.awt.Toolkit and the getSystemEventQueue() method.
Regards,
Manfred.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You guys are getting too complicated and for no reason at all. You seem to forget that all Components dispatch events to their respective listeners using the following methods in JComponent...
processComponentEvent( ComponentEvent)
processEvent( AWTEvent)
processFocusEvent( FocuseEvent)
...and so on.
Basically, when an event is generated from a JComponent, and for arguments sake, lets say it was a FocusEvent, the processFocusEvent(FocusEvent) method will send the event to all of the components FocusListeners, if any at all. So, if you override this mthod, you control the dispatching of events from your component to the respective listeners.
You need to subclass JTextField and override the approproate processEvent() methods to solve your problem. I can tell you this, you dont need to set up listeners because you will be catching the event too late if you do. By overriding tje processEvent() methods, you control the dispatching of events and therefore should be able to control focus, validation, etc.
hope that helps you,
SAF
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic