Close the application interface, but continue with the main () code

I am trying to close my main app UI but keep the code running in my main () function that started the app. Right now the problem is on my Mac, the program name stays in the Mac menu bar even though no windows are displayed.

So basically in the code that exits the application I:

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 launches the application looks like this:

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 were to use System.exit (0) it would completely stop the entire JVM and stop running stuff in the while loop. I can't figure out how to exit the main UI of the app, stop showing in the menu bar, but still run the while stuff.

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

+3


source to share


1 answer


It looks like this is not an easy way to do it. For those who have the same problem, there are several options:

1) Looks like other programs I do using the Macs taskbar (top right corner of the screen). The only way to access the program is through the menu on the taskbar. Even when you show the UI, you are taken to the UI via the taskbar. The downside to this is that when displaying the UI, you cannot use Cmd + Tab to navigate to the window. This is not intuitive for Mac users. If you want to use this parameter, you can run the java jar file with the command line parameter "-Dapple.awt.UIElement =" true "" and this will prevent the program from displaying the menu ALWAYS and then you will want to create a taskbar icon so the user can get into 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 user interface and the other without it. They can communicate using inter-process communication (IPC) using files, sockets, etc. If you don't have a lot of data to transfer between processes, this is a good solution.

3) You can probably use JNI to remove the menu in the application after closing all the user interfaces. But you need to dig into the Objective C Macs language. I cannot confirm that you can actually do this.

0


source







All Articles