Skip to main content

Application window focus

4 replies [Last post]
charlesabreu
Offline
Joined: 2004-10-23

Hi,

I have two questions:

1 - How to give focus to the current swing application? The user can be in a task on another app and sometimes I need to pop-up a window to gain her attention and wait for her action (often single key pressing). This pop-up window must own global focus and must *not* be modal. Is this possible?

2 - Is there any way to give focus to another application (native or swing) from a swing app?

ps: I don't know if this changes anything, but I'm runnning on Microsoft Windows.

Thanks,
Charles

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tarbo
Offline
Joined: 2006-12-18

1 - You can [url=http://java.sun.com/javase/6/docs/api/java/awt/Component.html#requestFocus()]request focus[/url] using the Component API. It will automatically attempt to focus the containing window, and focused windows may be on top of all others.

Keep in mind that aggressively asking focus is begging for your program to be closed. ;)

2 - I don't know of a way to 'yield' focus, or to pass it on to other components that you cannot access directly. Different applications run in different contexts, and those contexts are OS dependent. Different Swing applications run in different JVMs and therefore in different native contexts. Targetting specific components will require a good deal of OS-specific libraries.

charlesabreu
Offline
Joined: 2004-10-23

> 1 - You can
> [url=http://java.sun.com/javase/6/docs/api/java/awt/Co
> mponent.html#requestFocus()]request focus[/url] using
> the Component API. It will automatically attempt to
> focus the containing window, and focused windows may
> be on top of all others.
>

That seems not to work across applications boundaries. Or I'm using it the wrong way.

> Keep in mind that aggressively asking focus is
> begging for your program to be closed. ;)
>

You are right. But no need to worry here. Actually, user herself will ask for the window to pop-up. Only that she will ask it from an application other than mine, through a socked channel between the two applications. My application will most of the time remain iconifyied on the system tray until user ask for it.

> 2 - I don't know of a way to 'yield' focus, or to
> pass it on to other components that you cannot access
> directly. Different applications run in different
> contexts, and those contexts are OS dependent.
> Different Swing applications run in different JVMs
> and therefore in different native contexts.
> Targetting specific components will require a good
> deal of OS-specific libraries.

I thought this would be the response, but I was not sure. In fact, I hacked the thing in a OS-specific code, which solved the first problem too.

Thank you!

Charles Abreu

tarbo
Offline
Joined: 2006-12-18

Another option I only just thought of is new in Java 6: the TrayIcon and SystemTray. You can have a popup appear there in a system-dependent way. In particular, it does not rely on current focus, nor does it disturb the focal chain.

Then again, not [i]all[/i] OS'es support a system tray in some incarnation.

_simu_
Offline
Joined: 2005-01-28

Hello, if there's still interest, you can try cmdow (http://www.commandline.co.uk/cmdow/index.html) to select another window. It's a windows application, so you need to call it using Runtime.exec.
[]]]