I have searched for a long, long time to find a way to grab the hwnd, and on linux the Xid, of an awt canvas. I found all sorts of tricks that worked on older versions of java but apprently don't work anymore. Most of the things I read say that the only way to do this is to use JNI. I have come up with a way that works in java 1.5 without using jni and I want to know if it would work in other versions or if it should even be used since I override security and use a depricated call.
private long getHWND(Canvas c) {
try {
// grab the class that does all the action
Class cl = Class.forName("sun.awt.windows.WComponentPeer");
// find the hidden field
java.lang.reflect.Field f = cl.getDeclaredField("hwnd");
// expose the field
f.setAccessible(true);
// here is what we want
return f.getLong(c.getPeer());
}
catch (Exception ex) {
// something went wrong trying to expose the field we want
System.err.println("Fatal Error:");
return -1;
}
}
So what I want to know is will this work in other version of java and if it is safe to do it this way. It would be very similar for the Xid on linux systems too except I think I would need to call X11ComponentPeer.getContentWindow or something like that.