3

I have an application in java swing which runs in the system tray most of the time. The user can use the Tray Menu to perform several actions. One of those actions is to show the app window. The problem I have is with usability on OS X.

In windows, if the user brings up the app window and later switches to another app, they can switch back to my application using the taskbar. But in OS X, the app runs in a mode in which the app does not have a menu and also will not appear in the Command + Tab list.

__LSUIElement is set to true in the info.plist file

So my problem is that if the user opens the app window, later switches to another app, they cannot switch back unless they click on the window itself (which could be behind many other windows). The simplest way is for the user to open the window again using the tray icon, but since the window is already open I am unable to bring it to the front.

So, How to I take focus away from other applications and bring my window to the front?

EDIT:

My issue is not with getting the 'Application running as agent'. I already have that working. The issue I have is to get my program window to the top when its created. I am unable to do this in java.

Virat Kadaru
  • 2,216
  • 4
  • 23
  • 28

3 Answers3

1

You can call activateIgnoringOtherApps: with a true parameter when the user clicks on the icon in the menu bar (or however you open the window). You'll have to hook into Cocoa from Java, though.

mipadi
  • 398,885
  • 90
  • 523
  • 479
1

After much effort trying to do the same thing, I found that the following was the simplest solution for me:

try {
    String[] cmd = new String[2];
    cmd[0] = "open";
    cmd[1] = "/Applications/MyApp.app";

    Runtime.getRuntime().exec(cmd);
}
catch(Exception ex) {
    //ignore since there's nothing else that can be done
}

Essentially, I chose to execute another process through the JVM which uses the "open" command on Mac OS X to open the app that wraps the java program that created the tray icon. Telling it to open itself again causes the window to be brought to the front since it is already running.

Andrew Clark
  • 1,003
  • 9
  • 8
1

The code provided in the answer to the question below did the trick

How to bring a window to the front?

Community
  • 1
  • 1
Virat Kadaru
  • 2,216
  • 4
  • 23
  • 28