{"record":{"id":"b3a58daa75a64c78","repo":"java-native-access/jna","slug":"component-must-be-displayable","errorCode":null,"errorMessage":"Component must be displayable","messagePattern":"Component must be displayable","errorType":"exception","errorClass":"java.lang.IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Native.java","lineNumber":2549,"sourceCode":"    /** Provides separation of JAWT functionality for the sake of J2ME\n     * ports which do not include AWT support.\n     */\n    private static class AWT {\n        static long getWindowID(Window w) throws HeadlessException {\n            return getComponentID(w);\n        }\n        // Declaring the argument as Object rather than Component avoids class not\n        // found errors on phoneME foundation profile.\n        static long getComponentID(Object o) throws HeadlessException {\n            if (GraphicsEnvironment.isHeadless()) {\n                throw new HeadlessException(\"No native windows when headless\");\n            }\n            Component c = (Component)o;\n            if (c.isLightweight()) {\n                throw new IllegalArgumentException(\"Component must be heavyweight\");\n            }\n            if (!c.isDisplayable())\n                throw new IllegalStateException(\"Component must be displayable\");\n            // On X11 VMs prior to 1.5, the window must be visible\n            if (Platform.isX11()\n                && System.getProperty(\"java.version\").startsWith(\"1.4\")) {\n                if (!c.isVisible()) {\n                    throw new IllegalStateException(\"Component must be visible\");\n                }\n            }\n            // By this point, we're certain that Toolkit.loadLibraries() has\n            // been called, thus avoiding AWT/JAWT link errors\n            // (see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6539705).\n            return Native.getWindowHandle0(c);\n        }\n    }\n}\n","sourceCodeStart":2531,"sourceCodeEnd":2564,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Native.java#L2531-L2564","documentation":"Thrown by JNA's Native.getWindow(Component) when the given AWT Component is not displayable. A component becomes displayable only once it is connected to a native screen resource (added to a visible window hierarchy). JNA needs a real native window handle, so it refuses lightweight components and non-displayable ones.","triggerScenarios":"Calling Native.getWindow(c) before the component has been realized (frame.pack() not called or frame not shown), or calling it on a component that has been disposed.","commonSituations":"Querying the window handle during UI construction before setVisible(true)/pack(); calling after frame.dispose(); capturing window IDs in headless or early-init code.","solutions":["Ensure the component's window hierarchy is realized before calling Native.getWindow: call frame.pack() and frame.setVisible(true) first","Verify the component is actually added to a top-level container","Check that the component has not been disposed; recreate it if necessary","Wrap the call in a check: javax.swing.SwingUtilities.getWindowAncestor(c) != null and c.isDisplayable()"],"exampleFix":"// before\nlong hwnd = Native.getWindow(myPanel);\n// after\nframe.pack();\nframe.setVisible(true);\nif (myPanel.isDisplayable()) {\n    long hwnd = Native.getWindow(myPanel);\n}","handlingStrategy":"validation","validationCode":"if (c == null || c.isLightweight() || !c.isDisplayable()) {\n    throw new IllegalArgumentException(\"Component must be a displayable heavyweight\");\n}\nlong hwnd = Native.getWindow(c);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call Native.getWindow only after pack()/setVisible(true)","Check c.isDisplayable() before the call","Use SwingUtilities.getWindowAncestor to confirm a top-level window exists"],"tags":["jna","awt","swing","window-handle","lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}