简体   繁体   中英

How to alter WM_CLASS value in a Java GUI application based on Swing or NetBeans Platform?

All Swing/NetBeans-based Java GUI applications seem to have the same WM_CLASS value:

WM_CLASS(STRING) = "sun-awt-X11-XFramePeer", "java-lang-Thread"

This parameter can be viewed by issuing xprop command and pointing to the window. The practical purpose of customizing it is to let Mac-like docks (AWN, for example (and, perhaps, Ubuntu's Unity)) distinguish the application windows and group them under the application's pinned launcher icon. For this to work StartupWMClass parameter is to be set accordingly in the .application file in ~/.local/share/applications or /usr/share/applications . Needless to say, AWN (and analogues) get confused in case more than one application uses the same string for WM_CLASS .

This blog post found the field in Toolkit that controls it, named awtAppClassName . It suggests using reflection to modify it:

Toolkit xToolkit = Toolkit.getDefaultToolkit();
java.lang.reflect.Field awtAppClassNameField = xToolkit.getClass().getDeclaredField("awtAppClassName");
awtAppClassNameField.setAccessible(true);
awtAppClassNameField.set(xToolkit, applicationName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM