Forcing focus on (J)Frame

S

softwarepearls_com

Hi,

Does anyone know how to force the focus on a Frame (or JFrame) which
has already been realized and is currently showing? Ideally the
solution should be platform neutral, but I'll be happy with anything
which works on either mac or windows.

BTW, I searched the forum's discussions on this topic, but all the
articles I found were from pre 1.4 days (the focus system was
rearchitected for 1.4).

For the record, I've tried the following (which doesn't work on Mac):

private void ensureWindowHasFocus() {

try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {

// if not visible or not active, make it visible
(which makes it active)
if (!topLevelWindow.isVisible() || !
topLevelWindow.isActive()) {
topLevelWindow.setVisible(true);
}

topLevelWindow.toFront();
topLevelWindow.requestFocus();
boolean success =
topLevelWindow.requestFocusInWindow();
System.out.println("Req focus in Window: " +
success);
}
});
} catch (InterruptedException exception) {
exception.printStackTrace();
} catch (InvocationTargetException exception) {
exception.printStackTrace();
}

}
 
J

John B. Matthews

[...]
Does anyone know how to force the focus on a Frame (or JFrame) which
has already been realized and is currently showing? Ideally the
solution should be platform neutral, but I'll be happy with anything
which works on either mac or windows.

BTW, I searched the forum's discussions on this topic, but all the
articles I found were from pre 1.4 days (the focus system was
rearchitected for 1.4).

For the record, I've tried the following (which doesn't work on Mac):
[...]

It may not suit your purpose, but I recall something like this in the
context of JInternalFrame using setSelected(true):

<http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thre
ad/f3b21ed1083a3f75>
 
S

softwarepearls_com

[...]> Does anyone know how to force the focus on a Frame (or JFrame) which
has already been realized and is currently showing? Ideally the
solution should be platform neutral, but I'll be happy with anything
which works on either mac or windows.
BTW, I searched the forum's discussions on this topic, but all the
articles I found were from pre 1.4 days (the focus system was
rearchitected for 1.4).
For the record, I've tried the following (which doesn't work on Mac):

[...]

It may not suit your purpose, but I recall something like this in the
context of JInternalFrame using setSelected(true):

<http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thre
ad/f3b21ed1083a3f75>

Unfortunately, I'm not dealing with JInternalFrames.
 
J

John B. Matthews

softwarepearls_com said:
[...]> Does anyone know how to force the focus on a Frame (or JFrame) which
has already been realized and is currently showing? Ideally the
solution should be platform neutral, but I'll be happy with anything
which works on either mac or windows.
BTW, I searched the forum's discussions on this topic, but all the
articles I found were from pre 1.4 days (the focus system was
rearchitected for 1.4).
For the record, I've tried the following (which doesn't work on Mac):

[...]

It may not suit your purpose, but I recall something like this in the
context of JInternalFrame using setSelected(true):

<http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thre
ad/f3b21ed1083a3f75>

Unfortunately, I'm not dealing with JInternalFrames.

I understand. Mac OS and Windows have different ways of dealing with
multi-window applications. I had imagined that JInternalFrame might be
an alternative cross-platform approach.

Would JFrame's toFront() method work? I see it "brings this Window to
the front and may make it the focused Window." I appears to work on Mac
OS; I can see it not working on Linux for some window manager settings.
 
S

softwarepearls_com

 softwarepearls_com said:
<bd791526-fea4-4567-a30c-74efbc826...@q30g2000prq.googlegroups.com>,
[...]> Does anyone know how to force the focus on a Frame (or JFrame) which
has already been realized and is currently showing? Ideally the
solution should be platform neutral, but I'll be happy with anything
which works on either mac or windows.
BTW, I searched the forum's discussions on this topic, but all the
articles I found were from pre 1.4 days (the focus system was
rearchitected for 1.4).
For the record, I've tried the following (which doesn't work on Mac):
[...]
It may not suit your purpose, but I recall something like this in the
context of JInternalFrame using setSelected(true):
<http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thre
ad/f3b21ed1083a3f75>
Unfortunately, I'm not dealing with JInternalFrames.

I understand. Mac OS and Windows have different ways of dealing with
multi-window applications. I had imagined that JInternalFrame might be
an alternative cross-platform approach.

Would JFrame's toFront() method work? I see it "brings this Window to
the front and may make it the focused Window." I appears to work on Mac
OS; I can see it not working on Linux for some window manager settings.

After trying many more combinations (which all failed), I found a
solution with the help of a little Robot magic: Robot lets you post
native mouse events to the OS. So by forcing the window toFront(), and
then getting the window's coordinates and sending a mouse click to the
title bar (making sure I avoid the close/minimize/maximize buttons),
I'm able to reliably give the window the focus.

Pretty sad to have to perform such voodoo for something this simple,
but there you have it...
 
J

John B. Matthews

softwarepearls_com said:
After trying many more combinations (which all failed), I found a
solution with the help of a little Robot magic: Robot lets you post
native mouse events to the OS. So by forcing the window toFront(), and
then getting the window's coordinates and sending a mouse click to the
title bar (making sure I avoid the close/minimize/maximize buttons),
I'm able to reliably give the window the focus.

Pretty sad to have to perform such voodoo for something this simple,
but there you have it...

I'm not following: Which OS requires the voodoo?
 
S

softwarepearls_com

I'm not following: Which OS requires the voodoo?

I presume the Robot trick will work on all OSes which support Robot,
so unless I'm mistaken, Robot's features are supported on Windows/Mac/
Linux.
 
J

John B. Matthews

softwarepearls_com said:
I presume the Robot trick will work on all OSes which support Robot,
so unless I'm mistaken, Robot's features are supported on Windows/Mac/
Linux.

Using toFront() works fine to get focus on Mac OS, but I have yet to
test on Linux. Is this a Windows problem?
 
J

John B. Matthews

Sabine Dinis Blochberger said:
There is some issues with Windows XP (and presumably Vista), that
controls whether any window can "steal" the focus at any time.

Supposedly there's settings to this effect, but I never found them -
in our XMPP client application, on some Windows PCs, the newly opened
chat window fails to come to the front, although the taskbar bar will
blink.

So I've seen the effect, but I couldn't find a solution.

Ah, thank you. I'm guessing that cross-platform convenience might
explain an apparent preference for multi-paned applications (e.g.
NetBeans) or JInternalFrame (e.g. SQuirreL SQL).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top