public static void main(String[] args) throws Exception { JFrame alwaysOnTopFrame = new JFrame(\"Always on top Frame\"); alwaysOnTopFrame.setAlwaysOnTop(true); alwaysOnTopFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); alwaysOnTopFrame.setSize(500, 500); alwaysOnTopFrame.setVisible(true); alwaysOnTopFrame.setLocationRelativeTo(null); JOptionPane.showConfirmDialog(null, \"coucou\"); }
It displays a simple frame, with the "Always on Top" activated, then opens a modal dialog. What will happen? Which one will prevail and be in front of the other?
As it turns out, if you try to run this code, you will see the Frame hiding the modal dialog box, although the application waits for you to answer the dialog first. What will appear to the average user is that there is a frozen window which does not react to anything. Impossible to move it or close it, until you guess that there is a dialog box behind it.
Of course, if you know it, you can discard the dialog box using Enter or Escape key, or the Alt+F4 combination, or access the dialog box menu through Alt+Space and move it around using the arrow keys. But the average user won't know it.
What is the solution? There is actually an option that you can set on the Frame, so that it will still respond to user actions even though there are modal dialogs on the screen. It is called Modal Exclusion Type. Try adding the following line:
alwaysOnTopFrame.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);