Results 1 to 7 of 7
  1. #1
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default Getting the Window ID of a Window or Frame

    Hi!

    Is there a way to get the window ID of a (J)Frame or (J)Window? Specifically, I need it in order to dock MPlayer to a Java-frame so I can build my own GUI around it in order to embed MPlayer into an application I'm slowly putting together. Since getting the window ID is the first step, there's no real code to show, unfortunately, as I'm completely stumped on how to get it (if it's even possible).

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Do you mean you wish to get the control handle (hwnd) of the JFrame? If so, I'm no expert in this, but it can be done via JNA or a scripting program such as AutoIt.

    edit:
    For instance, the FindWindow function from the user32 library might fit your bill: FindWindow Function

    If I'm way off base, though, sorry, and please correct my wrong assumptions.
    Last edited by Fubarable; 05-14-2011 at 05:11 PM.

  3. #3
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    As far as I understand it, I only need the numerical ID of the window to let MPlayer dock with it. The rest of the docking procedure should be taken care of by MPlayer. Considering it's an argument sent to it through a command-line, it can't be much more complex than that, at least.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Toll View Post
    As far as I understand it, I only need the numerical ID of the window to let MPlayer dock with it. The rest of the docking procedure should be taken care of by MPlayer. Considering it's an argument sent to it through a command-line, it can't be much more complex than that, at least.
    I'm sorry, but I'm still confused by what you mean by "numerical ID". Is there any online documentation you can link to?

  5. #5
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    Ah, yes. Have a look at http://www.mplayerhq.hu/DOCS/man/en/mplayer.1.txt and search for "-wid <window ID>" (it's a looong doc). That's pretty much all the documentation there is regarding it, but hopefully it'll help at least this step.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Hm, I'm still not sure, and I don't know if what you're trying to do will work with a JFrame (the documentation states that it will work for a browser, but that's about all it states). Well regardless, if you need a window handle int, one way to get it is via JNA. The FindWindow API returns an HWND object, and it seems to me that the actual handle number can be obtained from the HWND hashCode. For example,

    Java Code:
    import com.sun.jna.*;
    import com.sun.jna.platform.win32.WinDef.HWND;
    import com.sun.jna.win32.*;
    
    public class GetWindowHandle {
    
       public interface User32 extends StdCallLibrary {
          User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);
    
          HWND FindWindow(String lpClassName, String lpWindowName);
       }
    
       public static int findWindow(String className, String windowName) throws WindowNotFoundException {
          HWND hwnd = User32.INSTANCE.FindWindow(className, windowName);
          if (hwnd == null) {
             throw new WindowNotFoundException(className, windowName);
          }
          return hwnd.hashCode();
       }
       
       @SuppressWarnings("serial")
       public static class WindowNotFoundException extends Exception {
          public WindowNotFoundException(String className, String windowName) {
             super(String.format("Window null for className: %s; windowName: %s", className, windowName));
          }
       }
    
       public static void main(String[] args) {
          try {
             // for a JFrame with a title of "Fubars Forever!"
             int winHandle = findWindow("SunAwtFrame", "Fubars Forever!");
             System.out.printf("Window Handle: 0x%X%n", winHandle); // show hex representation of handle
          } catch (WindowNotFoundException e) {
             e.printStackTrace();
          }
       }
    }

    Caveat: this is my code, and I'm no JNA expert but am just learning, so use with extreme caution.

    Also, this of course requires that JNA be downloaded and its libraries on the class pass or your IDE build list.
    Last edited by Fubarable; 05-14-2011 at 06:28 PM.

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    That worked like a charm. Well, aside from having the wrong dimensions and still lacking a proper GUI, but the biggest hurdle is past at least ;) Thanks a lot!

Similar Threads

  1. Replies: 4
    Last Post: 01-10-2010, 07:48 AM
  2. how can i move one frame window to another window
    By santhosh_el in forum AWT / Swing
    Replies: 8
    Last Post: 06-10-2009, 03:36 PM
  3. Regarding issue in frame window
    By santhosh_el in forum AWT / Swing
    Replies: 1
    Last Post: 06-01-2009, 08:10 AM
  4. i want to display decimal values in frame window
    By santhosh_el in forum AWT / Swing
    Replies: 4
    Last Post: 03-16-2009, 09:07 AM
  5. Read/Write of components of frame window
    By Harish kumara M in forum AWT / Swing
    Replies: 0
    Last Post: 09-17-2008, 08:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •