i know there is a lot of discussions around this question but this is different. We have a java application that show a JDialog on a Keyboard event on Windows (Native hook). It works fine, when i click something in the java window, the focus of the current application is not lost. On macOS, it change the active application to my java window. I managed to hide the dock icon with this :
-Dglass.taskbarApplication=false
But this is not enough, i want my java application to never be focused. I read about the headless property and it cannot work since i show a JDialog. It works perfectly in Windows but on Mac, a tray application is not the same i guess. Is this possible? Is the problem with the JDialog or can i add some argument to run my java application in the background? Thanks
Note: The solution below is tested on OS X only
Forcing the JDialog
to have a type of Window.Type#POPUP
as shown below seems to work.
dialog.setType(Window.Type.POPUP);
This allows the dialog to be focusable and when it gains focus other windows won't lose focus, like a popup menu. However, it also has other effects like making the dialog behave as if dialog.setAlwaysOnTop(true)
has been called.
Note: The example application below unfocuses other windows on startup but not after the unfocused windows are focused again.
Example:
import java.awt.EventQueue;
import java.awt.Window;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
public class NonUnfocusingDialogExample {
public static void main(final String[] args) {
EventQueue.invokeLater(() -> {
final JDialog dialog = new JDialog();
dialog.setType(Window.Type.POPUP);
dialog.getContentPane().add(new JLabel("Hello World!",
SwingConstants.CENTER));
dialog.pack();
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
dialog.setTitle("Test Dialog");
dialog.setLocationByPlatform(true);
dialog.setVisible(true);
});
}
}