Recently I was working on a swing application where I need to open an external Windows application from Java Swing JWindow.
Couple of problems I faced in this solution.
Problem # 1. Open an External Windows Application through Java Solution : This was easy, I just called
Problem # 2. Since there is a button on application to open Windows Application, so we need to make sure if an instance of that application is already open, then I shouldn't open another instance of same application. Solution : Wasn't so easy, but achieved with following solution. This method is working fine and indicating if this particular application is running or not.
Problem # 3. Now since application knows that an instance is running or not, in case of application instance is not running, I would execute , but in case application is already running, I want to simulate Alt-Tab functionality through Java so that open instance can be brought on top of my Java application.
Solution : ??? Need help/pointers.
The Best way to predict your future is to create it
Ankur Sharma
Ankur Sharma
Ranch Hand
Joined: Dec 27, 2005
Posts: 1234
posted
0
Started using java.awt.Robot class and coded in this way and this one worked.
But if someone else has any other way to achieve it, please post.
Using ALT+TAB like that won't necessarily work since it will display the next window. That doesn't necessarily have to be the window you want.
The right way would be to get hold of the right window, then send that to the front. I wouldn't know how to do that with VBS or something similar, but with JNI it's actually not that hard. You need to get hold of the process ID (add that to the printout of your VBS script and read it as well). You then use API call EnumWindows to iterate over all windows, using the process ID as the LPARAM argument; you need to create your own function for the first argument. For each window you check if the LPARAM value is equal to the result of GetWindowThreadProcessId. If so you've found the window. Once you have the handle to the window, you call SetForeGroundWindow to activate the window.
if you're happy using vbs, include in the code , if found - AppActivate
you'd need to look up its usage, I haven't used it for years.
Ankur Sharma
Ranch Hand
Joined: Dec 27, 2005
Posts: 1234
posted
0
Michael Dunn wrote:if you're happy using vbs, include in the code , if found - AppActivate
you'd need to look up its usage, I haven't used it for years.
Hmm... So what is this AppActivate?
Ankur Sharma
Ranch Hand
Joined: Dec 27, 2005
Posts: 1234
posted
0
Rob Spoor wrote:Using ALT+TAB like that won't necessarily work since it will display the next window. That doesn't necessarily have to be the window you want.
The right way would be to get hold of the right window, then send that to the front. I wouldn't know how to do that with VBS or something similar, but with JNI it's actually not that hard. You need to get hold of the process ID (add that to the printout of your VBS script and read it as well). You then use API call EnumWindows to iterate over all windows, using the process ID as the LPARAM argument; you need to create your own function for the first argument. For each window you check if the LPARAM value is equal to the result of GetWindowThreadProcessId. If so you've found the window. Once you have the handle to the window, you call SetForeGroundWindow to activate the window.
Sounds Interesting, let me try this option. I am not handy with GetWindowThreadProcessId, but let me give a try for this. Thanks for pointing out this.
You were correct that Robot solution wouldn't really help completely. But at-least as of now I have only 2 applications running Java application and Windows application.
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
> Hmm... So what is this AppActivate?
this is the very first 'hit' from google (the description on the search page)
AppActivate Function
msdn.microsoft.com/en-us/library/dyz95fhy(v=vs.80).aspx
Public Overloads Sub AppActivate( _ ByVal { Title As String | ProcessId As Integer } _ ) ...
You use AppActivate to bring an application's active window into focus.