{"record":{"id":"e0c5e4623481b544","repo":"java-native-access/jna","slug":"set-sun-java2d-noddraw-true-to-enable-transparent-windows","errorCode":null,"errorMessage":"Set sun.java2d.noddraw=true to enable transparent windows","messagePattern":"Set sun\\.java2d\\.noddraw=true to enable transparent windows","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/WindowUtils.java","lineNumber":805,"sourceCode":"            }\n        }\n\n        /** Return the last alpha level we set on the window. */\n        private byte getAlpha(Window w) {\n            if (w instanceof RootPaneContainer) {\n                JRootPane root = ((RootPaneContainer)w).getRootPane();\n                Byte b = (Byte)root.getClientProperty(TRANSPARENT_ALPHA);\n                if (b != null) {\n                    return b.byteValue();\n                }\n            }\n            return (byte)0xFF;\n        }\n\n        @Override\n        public void setWindowAlpha(final Window w, final float alpha) {\n            if (!isWindowAlphaSupported()) {\n                throw new UnsupportedOperationException(\"Set sun.java2d.noddraw=true to enable transparent windows\");\n            }\n            whenDisplayable(w, new Runnable() {\n                @Override\n                public void run() {\n                    HWND hWnd = getHWnd(w);\n                    User32 user = User32.INSTANCE;\n                    int flags = user.GetWindowLong(hWnd, WinUser.GWL_EXSTYLE);\n                    byte level = (byte)((int)(255 * alpha) & 0xFF);\n                    if (usingUpdateLayeredWindow(w)) {\n                        // If already using UpdateLayeredWindow, continue to\n                        // do so\n                        BLENDFUNCTION blend = new BLENDFUNCTION();\n                        blend.SourceConstantAlpha = level;\n                        blend.AlphaFormat = WinUser.AC_SRC_ALPHA;\n                        user.UpdateLayeredWindow(hWnd, null, null, null, null,\n                                                 null, 0, blend,\n                                                 WinUser.ULW_ALPHA);\n                    }","sourceCodeStart":787,"sourceCodeEnd":823,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/WindowUtils.java#L787-L823","documentation":"W32WindowUtils.setWindowAlpha only works when per-pixel alpha is available; isWindowAlphaSupported() returns false unless the DirectDraw/Direct3D pipeline is disabled via sun.java2d.noddraw=true (Java 6/7 era requirement). When unsupported, the method throws UnsupportedOperationException telling you to set that system property.","triggerScenarios":"Calling WindowUtils.setWindowAlpha(window, alpha) on Windows while -Dsun.java2d.noddraw=true is NOT set (default on Java 6 where it must be set; on Java 7+ the check may still fail in some configurations).","commonSituations":"Adding window-transparency to a Java 6 Swing app and forgetting the JVM flag; launching via javaw without VM args; embedding the app in a launcher (e.g. Launch4j/exe wrappers) that does not pass -Dsun.java2d.noddraw=true; conflicts with DirectDraw rendering causing flicker when the flag is absent.","solutions":["Launch the JVM with -Dsun.java2d.noddraw=true (must be set before any AWT/Swing initialization).","Set it programmatically as the very first line of main() before any Window is created: System.setProperty(\"sun.java2d.noddraw\", \"true\").","Alternatively use AWT's native setOpacity/setWindowOpacity (Java 7+) instead of WindowUtils when available.","Guard the call with WindowUtils.isWindowAlphaSupported() and degrade to opaque windows if false."],"exampleFix":"// before\nWindowUtils.setWindowAlpha(frame, 0.7f);\n// after\npublic static void main(String[] args) {\n    System.setProperty(\"sun.java2d.noddraw\", \"true\"); // before any AWT init\n    ...\n    WindowUtils.setWindowAlpha(frame, 0.7f);\n}","handlingStrategy":"validation","validationCode":"if (!WindowUtils.isWindowAlphaSupported()) { /* fallback */ } else { WindowUtils.setWindowAlpha(w, alpha); }","typeGuard":null,"tryCatchPattern":"try { WindowUtils.setWindowAlpha(frame, 0.7f); } catch (UnsupportedOperationException e) { // fallback to opaque or Java 7+ setOpacity }","preventionTips":["Always launch with -Dsun.java2d.noddraw=true when using WindowUtils alpha on Windows","Set the property programmatically before any AWT class loads","Prefer Java 7+ Window.setOpacity when your minimum JVM allows","Check isWindowAlphaSupported() before enabling transparency features"],"tags":["java","jna","swing","window-transparency","system-property"],"backgroundTag":"feature-not-enabled","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}