2


I'm using MPlayer in my java application. According to its doc, it's needed that I tell to MPlayer the window ID for embedding it. I'm doing this like that:

long winid = 0; //Window ID.
if (osName.startsWith("Windows")){
   final Class<?> cl = Class.forName("sun.awt.windows.WComponentPeer");
   java.lang.reflect.Field f = cl.getDeclaredField("hwnd");
   f.setAccessible(true);
   winid = f.getLong(overlay.getPeer()); //overlay is a canvas where MPlayer is embedded.
}
System.out.println(winid);

However, the getPeer() method is deprecated. I would want to know if there's a workaround for it.
Thanks a lot for the help.

bestsss
  • 11,796
  • 3
  • 53
  • 63
Manoel Afonso
  • 47
  • 1
  • 10
  • deprecated is wishful thinking; it's deprecated b/c it may not be portable but you do not care about. So what you do is right! Actually you dont need to use reflection. WComponentPeer has a public getHWnd(), so you need ((WComponentPeer) c.getPeer()).getHWnd() – bestsss Mar 09 '11 at 01:12

2 Answers2

1

I dropped a comment but that deserves an answer. Adding native code, MPlayer, so you are stuck with the impl and the OS. The deprecation of getPeer() is mostly b/c you can do really weird stuff with and not portable.

In your case it doesn't matter.

On a side note: WComponentPeer has a public getHWnd() method, so you do not need to trick it via reflection. The code you have now is actually pretty unsafe since it doesn't check for the actual peer.

you can replace it like that:

long hWnd = 0
try{ 
  Class clazz = Class.forName("sun.awt.windows.WComponentPeer);
  synchronized(overlay.getTreeLock()){
    ComponentPeer peer = overlay.getPeer();
    if (clazz.isInstance(peer)){
      hWnd = ((sun.awt.windows.WComponentPeer) overlay.getPeer()).getHWnd();
    }
  }
}catch(ClassNotFound _noWindows){
//process..
}

good luck!

bestsss
  • 11,796
  • 3
  • 53
  • 63
0

According to the documentation getPeer() has been replaced by isDisplayable() but this won't give you what you need. Apparently it is a violation of the spec to be accessing peers like you are (have a look here for more info).

If you absolutely must have the ID then you need another way to get hold of it because as I mentioned getPeer() has not even been replaced by some method that has similar functionality, it has now effectively become "private".

brent777
  • 3,369
  • 1
  • 26
  • 35
  • actually the OP is using `getPeer` exactly as it is supposed to be. The violation of the docs in the best case might make the code not portable but he is settled to obtain the hWnd (in windows), so it doesn't matter. – bestsss Mar 09 '11 at 01:14