*
The moose likes Swing / AWT / SWT and the fly likes How to simulate Alt-Tab functionality in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to simulate Alt-Tab functionality in Java" Watch "How to simulate Alt-Tab functionality in Java" New topic
Author

How to simulate Alt-Tab functionality in Java

Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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 - Every great individual common man
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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.

Appreciated your response.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19677
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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.
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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?
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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
> 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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to simulate Alt-Tab functionality in Java