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?
3 Answers
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

- 1
- 1

- 56,312
- 72
- 233
- 406
I was looking for a solution to a very similar problem.
Until now I have found one - very fragile! - solution.
It works for my case, because my case involves running entire sessions using java.awt.Robot
without user interaction, so it may - or may not - work for your case.
The solution is using java.awt.Robot
to send the key strokes like Alt+Tab
to bring the desired Window to the front.
This is fragile of course, due to multiple reasons:
- The Robot cannot know how often
Alt+Tab
needs to be send to get the desired window to front. The window is one in many. This solution only works if it's already known which window in terms ofAlt-Tab
-count it is. - Depending on the OS, the required keystrokes might actually be something else.
In case the OS and the window sequence are known, i.e. the program can know upfront how many Alt-Tab
keystrokes would be required, this could sometimes be a solution.
The following Java program demonstrates how to do this. If called without arguments, it generates exactly one Alt+Tab
keystroke. If called with arguments, the program assumes the first argument is a number and it will generate as many Alt+Tab
keystrokes as specified by that number.
You may want to play around a bit with the timing values given in robot.delay()
and robot.setAutoDelay()
in order to deliver the best experience on your machine. Hint: at least on Linux, robot.setAutoDelay()
should certainly be less than the keyboard repeat delay, otherwise the OS would generate multiple Alt-Tab
keystrokes in the system's event queue because of key repetition.
import java.awt.AWTException;
import java.awt.Robot;
import static java.awt.event.KeyEvent.VK_ALT;
import static java.awt.event.KeyEvent.VK_TAB;
import static java.lang.Integer.parseInt;
public class WindowSwitcher {
public static void main(final String... args) throws AWTException {
final int repeat = args.length != 0 ? parseInt(args[0]) : 1;
final Robot robot = createRobot();
robot.keyPress(VK_ALT);
for (int i = 0; i < repeat; i++) {
robot.keyPress(VK_TAB);
robot.keyRelease(VK_TAB);
robot.delay(500);
}
robot.keyRelease(VK_ALT);
}
public static Robot createRobot() throws AWTException {
final Robot robot = new Robot();
robot.setAutoWaitForIdle(true);
robot.setAutoDelay(10);
return robot;
}
}

- 17,035
- 5
- 40
- 47
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).

- 27,745
- 21
- 87
- 120