File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes TextField validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "TextField validation" Watch "TextField validation" New topic

TextField validation

Sean Scott

Joined: Jul 05, 2001
Posts: 2
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

Joined: Jun 21, 2001
Posts: 57
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.
Inorder to get the input value of the textfield which is to be vaildated. Use

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

Joined: Jul 05, 2001
Posts: 2
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.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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.
Ranch Hand

Joined: Jul 06, 2001
Posts: 257
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,
I agree. Here's the link:
subject: TextField validation
It's not a secret anymore!