3

I have two Java swing applications (means running in two JVM). Is there any way to switch between them? Active another application's window by Java code?

blackcompe
  • 3,180
  • 16
  • 27
icespace
  • 481
  • 9
  • 20
  • 2
    No, there isn't a way in pure Java – Dmitry B. Dec 07 '11 at 03:22
  • 2
    what do you mean by 'switch between them'? alt+tab will switch between them in once sense... – akf Dec 07 '11 at 03:24
  • 3
    See *Communication between local JVMs*: http://stackoverflow.com/questions/5052102/communication-between-local-jvms – blackcompe Dec 07 '11 at 03:45
  • 2
    See RMI (Remote Method Invocation): http://docs.oracle.com/javase/tutorial/rmi/index.html – Eng.Fouad Dec 07 '11 at 03:57
  • See `Launcher`, mentioned [here](http://stackoverflow.com/a/5696404/230513). – trashgod Dec 07 '11 at 05:06
  • `Runtime.getRuntime().exec("command to execute a proccess");` will run a process. Is this what you want? – Mohayemin Dec 07 '11 at 06:30
  • 2
    It seems that you want to interact with **already** running JVM process. So, I don't think `Launcher` or `ProcessBuilder` can fit your requirements since it will start a new JVM process. If this is a Windows-based Java Swing Application, you can use JNA to invoke User32 `FindWindow` function to search for a window with a given window title name and set its active state based on its process id. – ecle Mar 02 '12 at 11:27
  • @icespace Can you provide more details in your question so that others can understand what you are looking for and let this be off the unanswered list ? – prajeesh kumar Mar 07 '12 at 04:48

1 Answers1

3

You can try using JNA. I'll give you some code for Windows (more or less will be for other systems) using Maven: (sorry but I cannot get formatting right)

  1. Create Maven project, and add dependencies:

    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna</artifactId>
        <version>3.4.0</version>
    </dependency>
    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>platform</artifactId>
        <version>3.4.0</version>
    </dependency>
    
  2. Create interface

    public interface User32 extends StdCallLibrary {
        User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);
        HWND GetParent(HWND hWnd);
        HWND FindWindow(String lpClassName, String lpWindowName);
        HWND SetFocus(HWND hWnd);
        HWND FindWindowEx(HWND hwndParent, HWND hwndChildAfter, String lpszClass, String lpszWindow);
        int GetWindowText(HWND hWnd, char[] lpString, int nMaxCount);
    }
    
  3. Create class

    public final class Win32WindowUtils {
        private static final int WIN_TITLE_MAX_SIZE = 512;
    
        public HWND GetWindowHandle(String strSearch, String strClass) {
            char[] lpString = new char[WIN_TITLE_MAX_SIZE];
            String strTitle;
            int iFind = -1;
            HWND hWnd = User32.INSTANCE.FindWindow(strClass, null);
            while(hWnd != null) {
                User32.INSTANCE.GetWindowText(hWnd, lpString, WIN_TITLE_MAX_SIZE);
                strTitle = new String(lpString);
                strTitle = strTitle.toUpperCase();
                iFind = strTitle.indexOf(strSearch);
                if(iFind != -1) {
                    return hWnd;
                }
                hWnd = User32.INSTANCE.FindWindowEx(null, hWnd, strClass, null);
            }
            return hWnd;
        }
    }
    
  4. And invoke

    User32.INSTANCE.SetFocus(Win32WindowUtils.GetWindowHandle(windowTitle.toUpperCase(), null);
    

Of course - windowTitle is your window title (String) that you want to focus.

Java Devil
  • 10,629
  • 7
  • 33
  • 48
Xeon
  • 5,949
  • 5
  • 31
  • 52
  • Woo! Never expect anyone will take it seriesly. Since my request looks wired. Let me take some time to understand the code. Thank you so much!! – icespace Apr 19 '12 at 01:45
  • Actually you can clone my library (Mercurial) to look how things work: [library](https://bitbucket.org/pdrzewin/jnativehook/overview) – Xeon Apr 19 '12 at 08:44