Can I move another program's window to the front of focus?

11,239

Solution 1

You can't do it in pure Java code, but you could using JNI. See In Java Swing how do you get a Win32 window handle (hwnd) reference to a window? for how to get a handle to the window. Then you could do something like http://msdn.microsoft.com/en-us/library/ms633545 to move it to the front.

Caveat: This is for windows only

Solution 2

It's not possible in pure Java and on certain OSes it's not even possible to cleanly get the other windows position (for example good luck doing that under OS X 10.4: OS X 10.4 does not have any documented mean to registered for other windows' events... There are "hacks", but they're really hackish, like requiring the user to turn on the "Allow Assistive Device" preferences, which requires the OS X admin passord).

Share:
11,239
TwentyMiles
Author by

TwentyMiles

Updated on July 06, 2022

Comments

  • TwentyMiles
    TwentyMiles almost 2 years

    I am interacting with a third party application using it's API, and would like to move it to the front of focus (so that it is on top of all other open windows) when a user performs a certain action. While I can move my application up and down in the hierarchy pretty easily, there doesn't appear to be a way to interact with other windows. Is it possible to move another program's window to front with Java?