Search code examples
javajframewindownativehandle

Getting window handle in Java


I have been trying to get the current window pointer for a JFrame using the following code:

public class MainClass
{
    private JFrame mainFrame;
    private JPanel controlPanel;

    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setSize(400,400);
        jFrame.setVisible(true);

        try {
            jFrame.setVisible(true);
            final HWND hwnd = new HWND(Native.getComponentPointer(jFrame));
            System.out.println(hwnd);

        } finally {
            jFrame.dispose();
        }
    }

}

However, the result being outputted is always 'native@0x0'. What am I doing wrong?

Thank you!


Solution

  • What are you using windows or Mac? if Mac there is no way that you can get the pointer since this method only gets the pointer of windows from a windows machine.