Is it somehow possible to get a HWND of a JavaFX window in Java 9?

Deadly 提交于 2019-12-10 22:46:21

问题


Java 9 will restrict any access to private API. That means that the known methods of retrieving the window hwnd using Reflection won't work anymore.

Is there still a way to get them? I ask because I have a library that offers an API for manipulating the Taskbar (in a similar way that Java9 offers). The Java 9 API is still for AWT, so I hope I can get my project setup for Java 9 and JavaFX.

I used to just call the private methods, but this will stop working. Any solution is appreciated. Native calls are ok if they can be done using JNA or BridJ.


回答1:


One way could be to make changes to an already suggested solution to How can I get the window handle (hWnd) for a Stage in JavaFX? useful prior to JDK9 as :-

try {
    TKStage tkStage = stage.impl_getPeer();
    Method getPlatformWindow = tkStage.getClass().getDeclaredMethod("getPlatformWindow" );
    getPlatformWindow.setAccessible(true);
    Object platformWindow = getPlatformWindow.invoke(tkStage);
    Method getNativeHandle = platformWindow.getClass().getMethod( "getNativeHandle" );
    getNativeHandle.setAccessible(true);
    Object nativeHandle = getNativeHandle.invoke(platformWindow);
    return new Pointer((Long) nativeHandle);
} catch (Throwable e) {
    System.err.println("Error getting Window Pointer");
    return null;
}

with a module-info.java somewhat like :

module your.module {
    requires javafx.graphics;
}

But since javax.graphics exports the package internally to specific modules as:

exports com.sun.javafx.tk to
    javafx.controls,
    javafx.deploy,
    javafx.media,
    javafx.swing,
    javafx.web;

You can try adding a compiler option to still make use of it as

--add-exports javax.graphics/com.sun.javafx.tk=your.module

Note/Imp : Disclaimer from JEP-261:Module System -

The --add-exports and --add-opens options must be used with great care. You can use them to gain access to an internal API of a library module, or even of the JDK itself, but you do so at your own risk: If that internal API is changed or removed then your library or application will fail.



来源:https://stackoverflow.com/questions/38175031/is-it-somehow-possible-to-get-a-hwnd-of-a-javafx-window-in-java-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!