Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to Change focus of One window to another window. I have done this task in Windows and now I want to do same task for the Linux / Ubuntu OS using X11..

Suppose we have opened two applications

Runtime run = Runtime.getRuntime();
Process prog = run.exec("gedit");
Process prog2 = run.exec("gcalctool");


if current focused window is Calculator then Window's focus changed to the text Editor or vice versa.


Hey Experts Help...

Thanks in Advance

Regards
Nei Sa
Posted
Comments
wikus70 5-May-14 2:47am    
Can you show us the code for Windows?
Nei Sa 5-May-14 9:12am    
window = user32.FindWindow("notepad", null);
user32.SwitchToThisWindow(window,true);

I have used this one for Windows
wikus70 6-May-14 1:46am    
From the looks of it there is no Java library that will allow you to do this. That being said, there's nothing stopping you from using non-Java libraries. Have a look at these links. I hope it helps.
http://stackoverflow.com/questions/5206633/find-out-what-application-window-is-in-focus-in-java
http://msdn.microsoft.com/en-us/library/ms633505%28VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/ms633539(v=vs.85).aspx
http://codequirks.blogspot.com/2008/06/user32dll-functions-in-jni.html
Nei Sa 6-May-14 3:44am    
thanks.. yes it is not possible in Java but using JNA I have used user32 library..
But I am unable to do same task for the Linux...

Java
//Runtime run = Runtime.getRuntime(); // That's static stuff, please do not instance it.


Runtime.getRuntime().exec("gedit"); 
Runtime.getRuntime().exec("gcalctool"); 


Those commands are executing apps.
These commands do NOT change focus.
Only reason Calculator is in front of Text Editor is that it got activated last.
 
Share this answer
 
Comments
Nei Sa 5-May-14 9:12am    
I didn't said thats these statment will change the focus of windows.. I have supposed to be open these apps and want to change focus of these windows. :-)
I can't seem to find any library that you can import into Java to allow window switching on Linux.

What you can do is create a shell script to do the work for you and just call that shell script from your java application. The user needs "wmctrl" installed though.
http://askubuntu.com/questions/21262/shell-command-to-bring-a-program-window-in-front-of-another[^]

If your application needs to run on both Windows and Linux you can run a check to see what OS the application is running in.
Java
System.getProperty("os.name")

As for the shell scrip, you can try something like this. I haven't tested it yet as I don't have access to a Linux system. Also codeproject does not support shell scrip code so I hope this code will be readable.

#!/bin/bash
parameter1 = $1
wmctrl -a parameter1

In your Java application you can call this script like this:
Java
Process focusGedit = new ProcessBuilder( "path/to/script", "gedit").start();
 
Share this answer
 
Comments
Nei Sa 18-May-14 13:30pm    
@Wikus70

Thank you so much for your positive response. you have provide such a nice knowledge to me but I want to do this task without shell. I want to perform this task using X11.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900