Well, I know way that will almost certainly work and a way that 
might work.  Focus management has been buggy in Java from the 
start.  You would think this wouldn't be that hard to get right.

First the easy way that I think works.  When a window opens, the 
system is trying to set the focus on the first focusable component 
you have added to the window.  So make sure you add the Cancel 
button first thing.  The reason this might not work is that the 
other rule it might be using is the focusing on the top-left 
focusable component.  For the life of me I can't remember which 
rule Java uses.

Now for the slightly harder one.  You need to delay your 
requestFocus() until the system has already tried to focus on what 
it thinks should be focused.  What I generally do is implement a 
WindowListener and in the windowActivated (or windowOpened), do the 
requestFocus().  But wait!  That still doesn't work.  You need to 
delay it even further.  In the windowActivated method you need to 
create a Runnable class that does the requestFocus in the run() 
method.  Then (again in the windowActivated) you do a 
SwingUtilities.performLater(myRunnable).  This always delays the 
call enough so no one else overrules you.

Good luck.


On Wednesday, December 12, 2001, at 10:37  PM, Samivelu, Srinivasan 
wrote:

> Dear All
>
>
> I have dialog where there are four text fields and three buttons - 
> Default,
> Ok , Cancel buttons. When the dialog opens i want set focus for 
> the cancel
> button. which is the last button. At start the four text fields are non
> editable. on focus gained the text fields are set to editable 
> state. I have
> added an inner class implementing focus listener. i have set the
> requestFocus() to cancel button. Everything works fine. But when 
> dialog is
> opened for the first time the focus is set to the first text 
> field. but when
> i start the next time the request focus is set to the cancel button.
>
> How to overcome this. When dialog is opened for the first time i 
> want to set
> the focus to cancel button.
>
> wbr,
>
> Srinivasan Samivelu
> Software Development Engineer
>
> BarcoNet Private Limited.
> Mailto:[EMAIL PROTECTED]
> http://www.barconet.com
>
>
> _______________________________________________
> Advanced-swing mailing list
> [EMAIL PROTECTED]
> http://eos.dk/mailman/listinfo/advanced-swing
>
>
--
Bill Tschumy
Otherwise -- Austin, TX
[EMAIL PROTECTED]

_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to