Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a simple way to know if a modal dialog is currently displayed?

Is there one method in AWT or Swing to either tell me if there's a modal window (or multiple) up, or to return an array of them?

I looked in Window, Dialog, JDialog, SwingUtilities, etc. but couldn't find one.

(I know I can loop through Window#getWindows and check Dialog#isModal.)

like image 390
Geoffrey Zheng Avatar asked Feb 03 '23 01:02

Geoffrey Zheng


1 Answers

(This is what I know and works, though I'm not sure if it's correct to use Window#isShowing, or if I should use something else.)

public static boolean isModalDialogShowing()
{
    Window[] windows = Window.getWindows();
    if( windows != null ) { // don't rely on current implementation, which at least returns [0].
        for( Window w : windows ) {
            if( w.isShowing() && w instanceof Dialog && ((Dialog)w).isModal() )
                return true;
        }
    }
    return false;
}
like image 62
Geoffrey Zheng Avatar answered Feb 05 '23 14:02

Geoffrey Zheng