Is it possible to bring JFrame to front but NOT focus?

Johann Fridriksson picture Johann Fridriksson · Jan 25, 2011 · Viewed 14.3k times · Source

I'm writing a Java app (Swing GUI) that periodically pops up a JFrame.

Is it possible somehow to bring the window to front (foo.setAlwaysOnTop(true) would be even better) but without having it focus?

Some people move their eyes away from the screen from time to time to look at their keyboard while typing, and I'm sure that if this window would always capture the keyboard focus people would get really annoyed as it's causing them to lose quite a few keystrokes every time it pops up unnoticed.

In other cases, even when the user is actually capable of typing without looking at the keyboard all the time, having a window pop up and get focus could cause unwanted actions from the pop-up window itself (some Tab+Enter combination for example, where the user accidentally selects an option she really wouldn't had selected otherwise).

Thanks in advance!

Update

As Jonas suggests, foo.setFocusableWindowState(false); seems to work if called after the window has been rendered (tested on Gnome only).

This does not work:

foo.setFocusableWindowState(false);
foo.setVisible(true);
foo.setFocusableWindowState(true);

However, this does:

foo.setFocusableWindowState(false);
foo.setVisible(true);
Thread.sleep(1000);
foo.setFocusableWindowState(true);

I'll have to see if there's an event I can catch/listen to that allows me to do foo.setFocusableWindowStatue(true); when appropriate.

I consider my problem solved.

Answer

Jonas picture Jonas · Jan 25, 2011

This may work:

foo.setFocusableWindowState(false);