有没有一种简单的方法可以知道当前是否显示模态对话框?

Geo*_*eng 7 java swing modal-dialog awt

在AWT或Swing中是否有一种方法要么告诉我是否有模态窗口(或多个),或者返回它们的数组?

我看了看Window,Dialog,JDialog,SwingUtilities,等,但没能找到.

(我知道我可以循环Window#getWindows检查Dialog#isModal.)

Geo*_*eng 10

(这是我所知道和工作的,虽然我不确定它是否正确使用Window#isShowing,或者我是否应该使用别的东西.)

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;
}
Run Code Online (Sandbox Code Playgroud)