This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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?
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.
Joined: Jul 05, 2001
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
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