Search code examples
javamacoswindowfocus

In Java, how to keep the original window's focus, despite of clicking JFrame


I've some trouble in using this setFocusableWindowState(false) method in java:

JFrame frame = new JFrame(); frame.setFocusableWindowState(false); frame.setVisibled(true);

Even in Windows OS, it works definitely well. That is - when JFrame is visible, it keeps the focus of original window. So I could send some events such as Keypress to original window.

But, I'm still failing in Mac with same code. Just it lost his focus, however original window (showing before new JFrame window appears) lost also its focus. So I never can send any event to that window in Mac.

Please help me to keep the original window's focus in Mac OS X, though new JFrame appeared. Additionally, I used the method setAlwaysOnTop(true) in same time.


Solution

  • At last, I found the way to implement this in Mac.

    Actually, the window lost its focus when user click the JFrame by mouse left button.

    But when clicked by mouse right button, it never lost original focus. (I found this accidentally.)

    So, using JNI, I hooked all mouse events and then consume mouse left button click event. At the same time generated mouse right button click event in code.

    I hope you will get something from this answer. Best regards.