4

I have been searching all over the internet for this, going from stack overflow answer to stack overflow answer, Trying rococoa, or Class.forName ("com.apple.cocoa.application.NSApplication"); amongst other things.

The bottom line of the matter is, I cannot, for the love of god, figure out how to get my Java application to focus its self on OSX!

Let me be clear: My application has no windows (It will in the future, but sometimes it may not have any windows at all). I need a way to focus my application that does not rely on windows.

Having not found anything, I desperately decided to try a solution that relied on there being a window:

private static void BringSelfToFocus()
{
    java.awt.EventQueue.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            Window window = new JFrame("Test");
            window.toFront();
            window.repaint();
        }
    });
}

That, however, like every other futile attempt of mine, failed.

So, yes, while this is technically a duplicate question, I have tried every single other answer I could find, and for whatever reason, none of them worked.

Can anyone please lend a helping hand on this matter? Thankyou.

-Georges

1 Answer 1

6

Well! I had one final idea, and it worked! I used Applescript.

private static void BringSelfToFocus()
{
    AppleScript("tell me to activate");
}


private static String AppleScript(String script)
{
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("AppleScript");

    if (engine != null) {
        try
        {
            return (String)engine.eval(script);
        }
        catch (ScriptException e)
        {
            e.printStackTrace();
        }
    }
    return null;
}
Sign up to request clarification or add additional context in comments.

7 Comments

wow! I had a same problem and your solution worked perfectly! It's sad that toFront() doesn't work as it should, but glad there is this workaround. Thank you
Has anyone had success when this is combined with stackoverflow.com/a/11140556/3196753? I'm trying to add this logic to @Override public void setVisible(boolean b) without success. My dialogs are still behind other windows most of the time.
... I also tried tell application \"My App\" to activate (works from Terminal, but not from Java) as well as trying to fire it AFTER a WindowEvent.WINDOW_OPENED was triggered to no avail...
Answering my own question setVisible(...) is picky about order of operations. The super.setVisible(...) should be the last call in the function per github.com/qzind/tray/issues/367. Standard logic would place it the other way, but this seems to work well.
@tresf did you find a solution for bringing dialogs to front with apple.awt.UIElement set to true?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.