Results 1 to 6 of 6
  1. #1
    kbro3 is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default Swing components share FocusListener

    Hi All,

    I've been stumped by a FocusListener problem in Swing. I'm digging through someone else's code and basically, I have a scenario where one component somehow calls the FocusLost of another component (even though the second component never had focus to begin with).

    Is there a programmatic way to call FocusLost of a component? Eg - can I just invoke a FocusLost any old time?

    The interesting thing is that one component is a custom hybrid of a textfield with a calendar, let's call it a calendarField class (imagine a textfield which displays date, with a small button in it for selecting date).

    Pressing this button invokes FocusLost of an unrelated component. (it's not in the calendarField class, because there are several calendarFields in the dialog, and not all have this anamoly.)

    Any help / ideas appreciated!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Is there a programmatic way to call FocusLost of a component?

    May be I'm rushing in to an area of darryl's expertise...

    Do components have such a method? I thought components have focus listeners and they have a focusLost() method. That method is public, so feel free to call it. But beware because you need to pass it a FocusEvent instance.

    Maybe the focusLost() is doing something important like validating stuff. But the whole procedure strikes me as a bit hairy because you don't really have a proper argument to send focusLost(). You could pass null but this sounds like an odd way of doing whatever it is that this focus listener is doing.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    If you are attaching the same FocusListener to multiple components then inside the focusLost/Gained methods you need to determine which component lost/gained focus and only execute the appropriate code for that component.

  4. #4
    kbro3 is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    If you are attaching the same FocusListener to multiple components then inside the focusLost/Gained methods you need to determine which component lost/gained focus and only execute the appropriate code for that component.
    That's actually not the case here. Each component has it's own FocusListener. And those are NOT shared.

    Basically one component's function somehow calls another's focusLost() and I have no idea how..

    Thanks.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Typically coomponents have a whole array of focus listeners. Presumably the component in question gets this array (there's a method for that) and calls the focusLost() of each. But can't you just see from the code? Because if you don't have the code, how do you know focusLost() is being called and why do you care when, as I mentioned, it seems a weird misuse of focusLost() to be called this way.

    (I feel I'm setting myself up in the last part...)

  6. #6
    kbro3 is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Typically coomponents have a whole array of focus listeners. Presumably the component in question gets this array (there's a method for that) and calls the focusLost() of each. But can't you just see from the code? Because if you don't have the code, how do you know focusLost() is being called and why do you care when, as I mentioned, it seems a weird misuse of focusLost() to be called this way.

    (I feel I'm setting myself up in the last part...)
    Can't see from the code. Code is large and complex and I haven't familiarised myself with it. focusLost() is definitely being called, i've added a debug message to focusLost() code, so I know it's being called..

    Didn't know about the array of focus listeners, will investigate...

    Thanks!

Similar Threads

  1. Swing components not working
    By primalpop in forum AWT / Swing
    Replies: 30
    Last Post: 11-15-2009, 05:10 PM
  2. Swing Components Placing
    By ne2000 in forum Eclipse
    Replies: 2
    Last Post: 06-23-2008, 08:00 AM
  3. How to print Swing components
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM
  4. Where is it best to declare swing components?
    By MacNstuff in forum AWT / Swing
    Replies: 1
    Last Post: 02-06-2008, 12:59 AM
  5. HTML on Swing Components
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-27-2007, 09:51 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
  •