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
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...
----------
--
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
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
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.