Is there any way to bring a window to the front using Java? Maybe using some operating system library?
Asked
Active
Viewed 4,549 times
5
-
please correct the title – bluish Dec 17 '10 at 13:49
-
3what do you mean by activating a window , can you please elaborate ? – jmj Dec 17 '10 at 13:49
-
I fixed title, I think he means to bring a window to front, but I am not sure.. – Richard J. Ross III Dec 17 '10 at 13:51
-
He probably wants to call Win32's SetForegroundWindow from Java. JNI anyone ? – Janiek Buysrogge Dec 17 '10 at 13:53
-
3If "windows" is something that inherits `java.awt.Window`, `toFront` method brings it to the front of another windows in the current JVM. – khachik Dec 17 '10 at 14:05
-
2This is an unanswerable question as it is currently written. – Hovercraft Full Of Eels Dec 17 '10 at 14:19
-
Is "a window" referring to one that you have created and have control over in your program? Or is "a window" some random window on the same computer? Or are you asking about home automation software to open "a window" in your house? – Nate Dec 17 '10 at 17:15
-
Yes, i mean to bring a window to front, sorry for my bad English. – Lobo Dec 20 '10 at 08:54
-
Hi Nate, i mean to some random window on the same computer. – Lobo Dec 20 '10 at 08:56
3 Answers
4
It seems it is possible, but then your solution would be very OS specific.
Theoretically it can be done by placing a call to win32 API in the following sequence:
Now the problem comes 'how to call them from java?'. Well all the above functions are defined in user32.dll
and it can be accessed by JNA.
Some sample references to user32 API using JNA are:
Use google to find more.
Hope this will help.
-
-
Can you plz tell that how can we know which DLL is doing which kind of functions? is there any way to know it? – SmartSolution Apr 27 '11 at 15:22
2
package focus;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.win32.StdCallLibrary;
public class ForegroundWindow {
private interface User32 extends StdCallLibrary {
final User32 instance = (User32) Native.loadLibrary("user32", User32.class);
boolean SetForegroundWindow(HWND handle);
HWND FindWindowA(String className, String windowName);
HWND GetForegroundWindow();
}
private String getWindowName(String winName) {
String winText = "";
if (winText.contains(winName)) {
return winText;
}
return null;
}
public boolean bringWindowToFront(String className, String winName) {
HWND hWnd = User32.instance.FindWindowA(className, getWindowName(winName));
if (hWnd == null) {
return false;
}
return User32.instance.SetForegroundWindow(hWnd);
}
}

Gal Levy
- 39
- 3
0
SWT is nice for Win32 calls.
import org.eclipse.swt.internal.win32.OS;
@SuppressWarnings("restriction")
int hwnd = OS.FindWindowW(null, "Titlein".toCharArray());

Tag
- 286
- 2
- 5