Swing app global modal
Simple question:
Can the scan frame be fully modal (block all other windows)?
I tried the following, but I can still click on other app windows (like this browser)
JDialog myDialog = ....
myDialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
Insert some code if possible.
source to share
JFrame is not meant for modality. Use JDialog for this, but you will lose some of the JFrame functionality. If you can't live with the loss, you need to block the EventQueue and replace it with your own to only accept events from the blocker.
See Creating Modal Inner Frames for an explanation of using Inner Frames, which should also apply to JFrame.
Edit: Oups, my answer seems to be a little off the mark, as your example code shows that you are already using a Dialog subclass for this.
source to share
Dialogs don't have to be globally modal. Every modern OS strongly discourages global modality in HIG, and they might even drop functionality (as evidenced by the fact that you can't get it to work). Your application should never steal events from the entire system; that not only is bad design, it is almost criminal in my book.
Ignoring the fact that most people like to multitask between multiple apps, what about a scenario where you open a globally modal dialog and then your app freezes up? Ctrl + Alt + Del should work on Windows to kill the application, but I'm not sure about Cmd + Opt + Escape on Mac with a globally modal dialog (does Cocoa even have a global modal?). None of the Linux platforms have a good way to kill applications that have taken full control of the user interface as you suggest (you will have to kill X11 completely and start a fresh instance from scratch).
My answer is to find another way. I don't care what your client asks, they don't want it.
source to share
I don't know about global modality, but here's the idea.
- Take a screenshot of your desktop.
- In full screen mode.
- Complete the dialogue.
Since the desktop is a fake screenshot, you can ignore any attempt to click on it.
private void toggleFullScreenWindow() {
GraphicsEnvironment graphicsEnvironment
= GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice graphicsDevice
= graphicsEnvironment.getDefaultScreenDevice();
if(graphicsDevice.getFullScreenWindow()==null) {
dialog.dispose(); //destroy the native resources
dialog.setUndecorated(true);
dialog.setVisible(true); //rebuilding the native resources
graphicsDevice.setFullScreenWindow(dialog);
}else{
graphicsDevice.setFullScreenWindow(null);
dialog.dispose();
dialog.setUndecorated(false);
dialog.setVisible(true);
dialog.repaint();
}
requestFocusInWindow();
}
source to share