Results 1 to 5 of 5
- 02-16-2009, 09:58 PM #1
Member
- Join Date
- Feb 2009
- Location
- Askrigg, Yorkshire, England
- Posts
- 3
- Rep Power
- 0
find focus requester from input verifier
Can anyone tell me how to find out which component is requesting the focus when an InputVerifier for a text field is called?
Basically I'm writing a program using SI units so I have to cope with inputs which are a value + units pair. A text field holds the value and a combo box holds the possible units (like "mW", "W", "kW", "MW"). I only want to verify the input when the text field is exited to somewhere other than its associated combo box. I can't just prevent the combo box causing verification using combo.setVerifyInputWhenFocusTarget(false) because an attempt to move to that combo box from a different text field should fire the verification process for that field.
At the moment I'm contemplating using combo.setVerifyInputWhenFocusTarget(false) when its associated text field gains focus and combo.setVerifyInputWhenFocusTarget(true) when the text field loses focus but this seems inelegant. I'd like to do something inside the input verifier code itself if possible.
Any help will be much appreciated
- 02-17-2009, 03:40 PM #2
First, a warning about InputVerifier. It will not prevent button action performed events when the button is clicked or enter is pressed and a default button is assigned. This is a very annoying behavior, although it is useful for "cancel" type buttons.
I created PoliteButton that requests focus in the action performed listener and the invokes EventQueue.invokeLater() to call the method that actually processes the event. The first thing the method does is to check if the button has the focus.
I see what you are getting at, but I think your approach may end up creating so many scenarios that your code will be extremely complex.
Essentially,
- the value in the text field has no meaning apart from the setting in the combo box
- the value in the text field must be validated against some range criteria, along with being a valid number
Therefore, you must validate the value in the text box under two circumstances:
- the value in the text box is changed
- the value in the combo box is changed
First, I suggest putting the units ahead of the value on the screen. Your user needs to be able to set the units first to get a good validation.
Second, run the same validation logic for both components, pushing the focus back to the text field if the value is invalid.
- 02-17-2009, 05:37 PM #3
Member
- Join Date
- Feb 2009
- Location
- Askrigg, Yorkshire, England
- Posts
- 3
- Rep Power
- 0
Thanks for your interest Steve, I agree with much of what you say though putting the units before the number is a non-starter for a scientific program.
You haven't answered my direct question, however. When an input verifier is called, how do you find out which component is requesting the focus?
- 02-17-2009, 07:15 PM #4
Sorry. I don't believe you can. However, FocusEvent has the getOppositeComponent() method, which is what you want.
Here's another approach that is very functional when performing validations on fields with dependencies on each other. Simply wait to perform the dependent validations until the user indicates they have entered all the information by pressing the "Save" (or whatever) button.
While this approach is foreign to GUI developers, it is standard in green-screen and Web app programming. The advantage is that all the fields can be validated in an orderly manner, avoiding all the "what if" scenarios involved on focus-based validation. The disadvantage is really not that great. Most users don't enter invalid data except on occasion. In addition, their response to an error message is seldom to cancel what they were doing, they just fix the problem.
- 02-17-2009, 07:35 PM #5
Member
- Join Date
- Feb 2009
- Location
- Askrigg, Yorkshire, England
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
set focus to the textfield
By paty in forum New To JavaReplies: 2Last Post: 07-09-2010, 08:28 AM -
Setting focus
By carderne in forum New To JavaReplies: 4Last Post: 12-21-2008, 05:50 PM -
how to focus to another JTextfield?
By birdofprey in forum AWT / SwingReplies: 2Last Post: 04-09-2008, 01:08 PM -
how to take input and verify input in Java programs
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-21-2007, 08:46 AM -
Focus
By Marty in forum AWT / SwingReplies: 1Last Post: 05-31-2007, 02:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks