Results 1 to 5 of 5
  1. #1
    apothecary is offline Member
    Join Date
    Feb 2009
    Location
    Askrigg, Yorkshire, England
    Posts
    3
    Rep Power
    0

    Default 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

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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.

  3. #3
    apothecary is offline Member
    Join Date
    Feb 2009
    Location
    Askrigg, Yorkshire, England
    Posts
    3
    Rep Power
    0

    Default

    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?

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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.

  5. #5
    apothecary is offline Member
    Join Date
    Feb 2009
    Location
    Askrigg, Yorkshire, England
    Posts
    3
    Rep Power
    0

    Default

    I'm sorry to hear that you think it isn't possible to determine the component requesting the focus but it's pretty much what I had decided. I just hoped that someone might know how.

Similar Threads

  1. set focus to the textfield
    By paty in forum New To Java
    Replies: 2
    Last Post: 07-09-2010, 08:28 AM
  2. Setting focus
    By carderne in forum New To Java
    Replies: 4
    Last Post: 12-21-2008, 05:50 PM
  3. how to focus to another JTextfield?
    By birdofprey in forum AWT / Swing
    Replies: 2
    Last Post: 04-09-2008, 01:08 PM
  4. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM
  5. Focus
    By Marty in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2007, 02:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •