JNA unable to bring window to front when application is in background

1,342 views
Skip to first unread message

Rajan Dhir

unread,
Oct 26, 2015, 7:07:58 AM10/26/15
to Java Native Access

I have a javafx application for Windows which launches a selenium chrome window Here is the process flow:

  1. On application launch , user is greeted with a login box.
  2. After entering his details the mainStage is shown.
  3. A selenium chrome window is launched immediately.

At this point in time the chrome window has system focus. I would like the java application to have focus instead. Yes the user can use Alt + tab but I would want the user to be able to interact with my java application immediately. Things I have tried:

  1. Get HWND for both my app and the chrome window
  2. Use setForegroundWindow on my application window

However this only causes the icon on the taskbar to flash but the focus is still with the selenium window. Here is the code snippet.

if (PlatformSpecific.isOnWindows()) {
        // Restores browser window if it is minimized / maximized
        user32.ShowWindow(browserWindowHandle, WinUser.SW_SHOWNOACTIVATE);
        // SWP_NOMOVE and SWP_NOSIZE prevents the 0,0,0,0 parameters from taking effect.
        logger.info("Bringing bView to front");
        boolean success = user32.SetWindowPos(browserWindowHandle, mainWindowHandle, 0, 0, 0, 0,
                SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
        if (!success) {
            logger.info("Failed to bring bView to front.");
            logger.info(Kernel32.INSTANCE.GetLastError());
        }
        user32.SetForegroundWindow(mainWindowHandle);
    }

This code is called right after the chrome window is launched. But it always fails to bring my main application to foreground with error code 1400. I think it has something to do with the java thread unable to execute this being in the background. Any help would be much appreciated !


L Will Ahonen

unread,
Oct 27, 2015, 3:50:38 AM10/27/15
to Java Native Access
Hi,

Windows mostly doesn't allow applications to steal focus anymore. https://msdn.microsoft.com/en-us/library/windows/desktop/ms633539%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396 has a nice list of conditions where you are allowed to take focus programmatically.

From a quick read of your post, I don't think you will be able to make this happen.

BR,
Will
Reply all
Reply to author
Forward
0 new messages