Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How/when does requestFocus work?

0 views
Skip to first unread message

Chris Rieser

unread,
Apr 5, 1999, 3:00:00 AM4/5/99
to
I am trying to set the focus to the first field in
a dialog. I have tried using field.requestFocus()
in the constructor for the JDialog and no success.

I have also addWindowListener to the Dialog and
added the field.requestFocus() in the windowOpened
method of the listener class . Same (non) result.

The focus appears to be either nowhere or on the
dialog window itself, because a tab will take it
to the field and from there on tabbing traversal
works just fine. But I want the focus in the correct
field to start with.

Am I doing this wrong, or just in the wrong place/
wrong time?

TIA,
Chris

mark

unread,
Apr 5, 1999, 3:00:00 AM4/5/99
to
it may be that the jdialog sets the focus be default sometime after it
looks at your requestFocus call in the constructor. i would experiment
with putting the method call somewhere else, if possible. but i'm just
guessing.
mw

Mike

unread,
Apr 5, 1999, 3:00:00 AM4/5/99
to
The component must be realized before requestFocus() will work on it.
This means that placing it in the constructor will not work. I use
windowOpened() all over the place without a problem. If this isn't
working for you, call requestFocus() inside of
SwingUtilities.invokeLater(). I've had to do this when changing focus
from within an event handler for some reason. I've got theories as to
why, but haven't bothered to dig through the source.

Another useful trick is to override requestDefaultFocus() to set focus
to your component, though I don't think this will make a difference in
your case.

Mike

Chris Rieser wrote:
>
> I am trying to set the focus to the first field in
> a dialog. I have tried using field.requestFocus()
> in the constructor for the JDialog and no success.
>
> I have also addWindowListener to the Dialog and
> added the field.requestFocus() in the windowOpened
> method of the listener class . Same (non) result.
>
> The focus appears to be either nowhere or on the
> dialog window itself, because a tab will take it
> to the field and from there on tabbing traversal
> works just fine. But I want the focus in the correct
> field to start with.
>
> Am I doing this wrong, or just in the wrong place/
> wrong time?
>
> TIA,
> Chris

--
----------
Reply to co...@interzone.com...
----------
--

Mr. Tines

unread,
Apr 6, 1999, 3:00:00 AM4/6/99
to
###

On Mon, 5 Apr 1999 16:32:01 -0400, in
<7eb62v$14uk$1...@rtpnews.raleigh.ibm.com>
"Chris Rieser" <cri...@us.ibm.com> wrote.....

> I am trying to set the focus to the first field in
> a dialog. I have tried using field.requestFocus()
> in the constructor for the JDialog and no success.

With sufficiently late versions of Swing and JDK, the
initial focus should go to the first component added
to the dialog. Howveer, in practice, this means you have
to do it by hand - my usual trick is, where focus is the
component to get initial focus, use this override to
JDialog.show() when that is called from the event thread

public void show()
{
// neat trick - this works because it
// happens after the show (which is in the handling
// for this event), but before it returns

if(focus != null)
{
SwingUtilities.invokeLater( new Runnable() {
public void run () {
focus.requestFocus();
}});
}

// now show it
super.show();
}


-- PGPfingerprint: BC01 5527 B493 7C9B 3C54 D1B7 248C 08BC --
_______ {pegwit v8 public key =581cbf05be9899262ab4bb6a08470}
/_ __(_)__ ___ ___ {69c10bcfbca894a5bf8d208d001b829d4d0}
/ / / / _ \/ -_|_-< www.geocities.com/SiliconValley/1394
/_/ /_/_//_/\__/___/@windsong.demon.co.uk PGP key on page

### end pegwit v8 signed text
28fe919e9c2d10be5de98f99baf575088bd01d03a3f6ce1fb225e65d7ce4
b32b35d1560f23985c4c258b29b821d0127b943c78d956fb2810af62af0f


Kausik Saha

unread,
Apr 8, 1999, 3:00:00 AM4/8/99
to
Here is a piece of code that I use and it works fine. The problem is that
requestFocus() works only when the component is visible. So you have to add a
focusListener to your dialog and give a call to requestFocus() of the
component you want when your dialog gains focus.

addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
c.requestFocus(); // c is the component you want to focus
} // end if
} // focusGained
}); // addFocusListener

Hope this helps

Kausik Saha

In article <7eb62v$14uk$1...@rtpnews.raleigh.ibm.com>,


"Chris Rieser" <cri...@us.ibm.com> wrote:
> I am trying to set the focus to the first field in
> a dialog. I have tried using field.requestFocus()
> in the constructor for the JDialog and no success.
>

> I have also addWindowListener to the Dialog and
> added the field.requestFocus() in the windowOpened
> method of the listener class . Same (non) result.
>
> The focus appears to be either nowhere or on the
> dialog window itself, because a tab will take it
> to the field and from there on tabbing traversal
> works just fine. But I want the focus in the correct
> field to start with.
>
> Am I doing this wrong, or just in the wrong place/
> wrong time?
>
> TIA,
> Chris
>
>

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Kausik Saha

unread,
Apr 9, 1999, 3:00:00 AM4/9/99
to
This thing works for me. Try it out.

You have to add a focusListener to your dialog. This is required as
requestFocus works only when the component is visible.

addFocusListener(new FocusAdapter(){
public void focusGained(FocusEvent e)
{

myComponent.requestFocus();
}
});

Hope this works.

0 new messages