Geoffrey Zheng
Geoffrey Zheng

Reputation: 6638

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.)

Upvotes: 9

Views: 4523

Answers (1)

Geoffrey Zheng
Geoffrey Zheng

Reputation: 6638

(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;
}

Upvotes: 12

Related Questions