Search code examples
javaswingmodal-dialogjframewindowlistener

Notification if a modal dialog is shown on top of a JFrame


What listener should I register in a JFrame instance to be notified if a modal JDialog is shown on top of the frame (the frame is the owner of the dialog)? Thanks in advance.


Solution

  • I think JFrame.addWindowListener(...) would work and then pay attention to WindowListener.windowDeactivated(...)

    ETA:

    jFrame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowDeactivated(WindowEvent e) {
                if(e.getOppositeWindow() instanceof JDialog) {
                    JDialog dialog = (JDialog) e.getOppositeWindow();
                    if(dialog.isModal()) {
                        // do stuff
                    }
                }
            }
        });