Results 1 to 6 of 6
- 03-18-2011, 05:28 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
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!
- 03-18-2011, 05:38 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 03-18-2011, 05:41 AM #3
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.
- 03-18-2011, 05:48 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
- 03-18-2011, 05:55 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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...)
- 03-18-2011, 06:08 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
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
-
Swing components not working
By primalpop in forum AWT / SwingReplies: 30Last Post: 11-15-2009, 05:10 PM -
Swing Components Placing
By ne2000 in forum EclipseReplies: 2Last Post: 06-23-2008, 08:00 AM -
How to print Swing components
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:04 PM -
Where is it best to declare swing components?
By MacNstuff in forum AWT / SwingReplies: 1Last Post: 02-06-2008, 12:59 AM -
HTML on Swing Components
By Java Tip in forum Java TipReplies: 0Last Post: 11-27-2007, 09:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks