3

I'm trying to close my main application user interface, but leave code running in my main() function that launched the application. Right now the problem I have is on a Mac the program name remains in Mac's menu bar even though there are no windows shown.

So basically in the code that would exit the application I have:

private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
    //System.exit(0);
    this.setVisible( false );

    // Do something here to finish closing application.
}

The main function that starts the application looks like:

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            // NewApplication is a javax.swing.JFrame
            new NewApplication().setVisible(true);
        }
    });

    while (true) {
        // Watch for user to relaunch UI and do lots of other tasks.
    }
}

If I used System.exit(0) it would stop the entire JVM completely and stop running the stuff in the while loop. I cannot figure out how to exit the main application UI, stop from showing in the menu bar, but still run the while loop stuff.

The reason I'm trying to do this is I need something that will run continuously and sometimes the user will need to run a user interface that interacts with the stuff that is running. The stuff inside the while loop checks to see if they are trying to launch the user interface again (among other functions) and would reload it. One option is to make one program that runs continuously and use inter-process communication to talk between the user interface and a non-UI program, but I would need to pass lots of data back and forth so I don't like that option.

  • 1
    http://stackoverflow.com/a/1235994/1291150 Is this what you're looking for? – Bohuslav Burghardt Dec 27 '14 at 21:13
  • setDefaultCloseOperation(JFrame.DoNothing) I don't remember the const name :) – TacB0sS Dec 27 '14 at 21:20
  • There's an API called Apple Java Extensions that lets you control how Java programs interact with the OSX window manager. It still exists, but Apple has deprecated it and helpfully scrubbed all traces of documentation from their developer site. The best way to learn about it might be to study an open source stub library like Orange Extensions. – Kevin Krumwiede Dec 27 '14 at 21:23
  • Thanks but `this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));` closes the program completely (including the while loop), and I should also mention that `this.dispose()` leaves the application open in the Mac menu bar. `frame.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );` also leaves the name of the application in the Mac menu bar (after selecting the exit menu). – Brandon Drake Dec 27 '14 at 21:24
  • Why do you expect that the application won't be on the menu bar if your intention is for it to remain running? – Kevin Krumwiede Dec 27 '14 at 21:26
  • It should remain running just like a service or daemon process. It's just I don't want the user to be bothered with having to leave a UI open. – Brandon Drake Dec 27 '14 at 21:28
  • If you don't want icons in the menubar, (I maybe wrong here but) what about Dialogs? perhaps JDialogs? – DreadHeadedDeveloper Dec 28 '14 at 06:10
  • The problem is I do want the icon in the menubar when the UI is shown so that the user can Cmd+Tab over to the program and easily get to it when the UI is shown. When the UI is not shown, we want it hidden. I can prevent it from showing in the menubar all the time but that would prevent it from being in the menubar when the UI is shown, which isn't what I'd like. – Brandon Drake Dec 28 '14 at 16:44

1 Answers1

0

It appears there is not an easy way of doing this. For those that have the same problem here are a few options:

1) It looks like other programs I have do this by using Mac’s task bar (in the upper right corner of the screen). The only way you access the program is through a menu on the task bar. Even when you have UI’s shown you get to the UI through the task bar. The downside of doing this is that when the UI is shown you can’t use Cmd+Tab to get over to the window. This is non-intuitive for Mac users. If you want to use this option you can start the java jar file with the command line option “-Dapple.awt.UIElement="true”” and that will prevent the program from showing a menu ALWAYS, and then you'll want to create a task bar icon so the user can get to your program.

See How to hide the Java SWT program icon in the Dock when the application is in the tray

2) Have 2 programs that run, one with a UI and another without. They can communicate using interprocess communication (IPC) using files, sockets, etc. If you don’t have much data to pass between the processes, this is a good solution.

3) You could probably use JNI to remove the menu on the application after all the UI’s close. But you’ll need to dig into Mac’s Objective C language. I can't confirm you can actually do this though.

Community
  • 1
  • 1